Мне интересно, если я собираюсь сделать это неправильно. Я начинаю входить в классы и пытаюсь использовать их больше в моем коде VBA.Класс, возвращающий (частный?) Диапазон
Я создал код, который импортирует CSV-файл на вновь созданный рабочий лист. Я создаю рабочий лист внутри класса и импортирую его в некоторый диапазон. Диапазон варьируется в зависимости от импортируемого файла. В идеале я хотел бы вернуть ссылку диапазона на мой модуль за пределами класса и циклически перемещаться по диапазону, как мне было нужно.
Не могу показаться, что я не могу вернуть объект диапазона с кодом модуля. с:
' Module code '''' \/
set NewTest as new TestClass
Dim testrange As Range
test Range = NewTest.SourceRange
' Module code '''' /\
Option Explicit
Const cDirectory As String = "C:\directory" 'file path
Const cFileExt As String = ".CSV" 'file
Const cSourceCode As String = "wsSourceCode"
Private cFinalFileName As String
Private wsSourceCode As Worksheet
Private cSourcePath As String
Private pSourceRange As Range
Private LastRow As Integer
Private pSourceSheet As String
'''''''''''''''''''''''''''''''''''''''''''''''''''' Can i do this !?!?!?!? \/
Public Property Get SourceRange() As Range
LastRow = wsSourceCode.Cells(wsSourceCode.Rows.Count, "A").End(xlUp).Row
Set pSourceRange = wsSourceCode.Range("A1:A" & LastRow)
SourceRange = pSourceRange
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''' Can i do this !?!?!?!? ?|
Public Property Get SourceSheet() As String
pSourceSheet = wsSourceCode.Name
SourceSheet = pSourceSheet
End Property
Public Property Get FinalRange() As String
FinalRange = pFinalRange
End Property
Public Property Set FinalRange(Value As Range)
pFinalRange = Value
End Property
Public Property Let SourceFile(Value As String)
pSourceFile = Value
cFinalFileName = pSourceFile & "Production" & cFileExt
cSourcePath = cDirectory & pSourceFile & cFileExt
End Property
Private Sub Class_Initialize()
'Create Holding Sheet to Populate and augment Code
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets(cSourceCode).Delete
On Error GoTo 0
Application.DisplayAlerts = True
ActiveWorkbook.Worksheets.Add(After:=Worksheets(1)).Name = cSourceCode
Set wsSourceCode = Sheets(cSourceCode)
'WsHolding.Visible = False
End Sub
Private Sub Class_Terminate()
' Application.DisplayAlerts = False
' On Error Resume Next
' ThisWorkbook.Sheets(cSourceCode).Delete
' On Error GoTo 0
' Application.DisplayAlerts = True
End Sub
Public Sub ExportCode(ByVal pFinalRange As Range)
''''''''''''''''''''''''''''''''''''''''''''''''''''
'This Process Will import the final Range of output
'code and export it to a Output file.
' The Output file was delare earlier.
'
'
''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim fso As Object
Dim Fileout As Object
Dim myFile As String
Dim rng As Range
myFile = cOutputFile
Open myFile For Output As #1
For Each rng In pFinalRange
Print #1, rng
Next rng
Close 1
End Sub
Public Sub ImportFile(ByVal cSourceFileName As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''
'This Process Will import the final Range of output
'code and export it to a Output file.
' The Output file was delare earlier.
'
'
''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim strTextLine As String
Dim cSourcePath As String
cSourcePath = cDirectory & cSourceFileName & cFileExt
Dim iFile As Integer: iFile = FreeFile
Dim i As Integer: i = 1
Open cSourcePath For Input As #iFile 'open the file
Do Until EOF(1) 'until the end of the file
Line Input #1, strTextLine 'set each line equal to variable strTextLine
wsSourceCode.Cells(i, 1).NumberFormat = "@"
wsSourceCode.Cells(i, 1) = CStr(strTextLine)
i = i + 1
Loop
Close #iFile
End Sub
«Диапазон» - это объект, поэтому вам нужно использовать ключевое слово 'Set' везде, где вы его назначаете. – Comintern