我创建了这个名为“CInventory”的类。该类生成一个“有效数据”数组。为了在一个范围内看到这个数组,我试图“粘贴”数组以查看结果。我得到一个“下标超出范围”。该错误出现在简单模块的第6行(cinv.AllocateInventoryData)上。
这是名为CInventory的类模块:
Option Explicit
Private Path As String
Private TextFileNumber As Integer
Private InventoryData() As String
Public Property Get GetInventoryData() As String
GetInventoryData = InventoryData
End Property
Public Property Let SPath(Text As String)
Path = Text
End Property
Public Sub AllocateInventoryData()
Dim ColumnArray() As String
TextFileNumber = FreeFile
Dim FileContent As String
Dim i, j As Integer
j = 0
Open Path For Input As TextFileNumber
FileContent = Input(LOF(TextFileNumber), TextFileNumber)
Close TextFileNumber
ColumnArray() = Split(FileContent, vbCrLf)
For i = LBound(ColumnArray) To UBound(ColumnArray)
If IsValidRow(ColumnArray(i)) Then
ReDim Preserve InventoryData(j, 4)
InventoryData(j, 0) = Left(ColumnArray(i), 8)
InventoryData(j, 1) = Trim(Mid(ColumnArray(i), 10, 8))
InventoryData(j, 2) = Trim(Mid(ColumnArray(i), 19, 18))
InventoryData(j, 3) = Trim(Mid(ColumnArray(i), 57, 2))
InventoryData(j, 4) = Trim(Mid(ColumnArray(i), 60, 12))
j = j + 1
End If
Next i
End Sub
Private Function IsValidRow(Text As String) As Boolean
If Left(Text, 6) = "iclorp" Or Left(Text, 5) = "Page:" Or Len(Trim(Text)) < 3 _
Or Left(Text, 4) = "Site" Or Left(Text, 6) = "------" Then
IsValidRow = False
Else
IsValidRow = True
End If
End Function
这是我称之为CInventory类(简单模块)的模块
Option Explicit
Sub example()
Dim cinv As CInventory
Set cinv = New CInventory
cinv.SPath = "H:\Joel\invent_2.prn"
cinv.AllocateInventoryData '<-- here appears the error
Worksheets(1).Cells(1, 1) = cinv.GetInventoryData
End Sub
答案 0 :(得分:0)
这是答案!过了一会儿,但它按照我想要的方式工作
课程模块'CInventory'
Option Explicit
Private Path As String
Private TextFileNumber As Integer
Private InventoryData() As String
Private UBoundInventoryDataRows As Integer
Private UBoundInventoryDataColumns As Integer
Public Property Get GetUBoundInventoryDataRows() As Integer
GetUBoundInventoryDataRows = UBoundInventoryDataRows
End Property
Public Property Get GetUBoundInventoryDataColumns() As Integer
GetUBoundInventoryDataColumns = UBoundInventoryDataColumns
End Property
Public Property Get GetInventoryData() As String()
GetInventoryData = InventoryData
End Property
Public Property Let SPath(Text As String)
Path = Text
End Property
Public Sub AllocateInventoryData()
Dim tempArray() As String
Dim TempLineArray() As String
Dim ColumnArray() As String
Dim FileContent As String
Dim i, j As Integer
TextFileNumber = FreeFile
j = 0
Open Path For Input As TextFileNumber
FileContent = Input(LOF(TextFileNumber), TextFileNumber)
Close TextFileNumber
ColumnArray() = Split(FileContent, vbCrLf)
For i = LBound(ColumnArray) To UBound(ColumnArray)
If LCase(Left(Trim(ColumnArray(i)), 2)) = "mx" Then
ReDim Preserve tempArray(j)
tempArray(j) = Trim(Left(ColumnArray(i), 9)) & ":" & _
Trim(Mid(ColumnArray(i), 10, 9)) & ":" & _
Trim(Mid(ColumnArray(i), 19, 18)) & ":" & _
Trim(Mid(ColumnArray(i), 37, 20)) & ":" & _
Trim(Mid(ColumnArray(i), 57, 4)) & ":" & _
Trim(Mid(ColumnArray(i), 61, 12)) & ":" & _
Trim(Mid(ColumnArray(i), 73, 9)) & ":" & _
Trim(Mid(ColumnArray(i), 82, 9)) & ":" & _
Trim(Mid(ColumnArray(i), 91, 8)) & ":" & _
Trim(Mid(ColumnArray(i), 99, 6)) & ":" & _
Trim(Mid(ColumnArray(i), 105, 9)) & ":" & _
Trim(Mid(ColumnArray(i), 114, 6)) & ":" & _
Trim(Mid(ColumnArray(i), 120, 4)) & ":" & _
Trim(Mid(ColumnArray(i), 124, 3))
j = j + 1
End If
Next i
'in a normal module we shall transpose this next array.
'I did this in this way because of redim agreement
' "the last dimension only could be re-dimensioned"
ReDim InventoryData(13, UBound(tempArray))
For i = LBound(tempArray) To 13
For j = LBound(tempArray) To UBound(tempArray)
TempLineArray = Split(tempArray(j), ":")
InventoryData(i, j) = TempLineArray(i)
Next j
Next i
UBoundInventoryDataRows = UBound(InventoryData, 1)
UBoundInventoryDataColumns = UBound(InventoryData, 2)
End Sub
这里是简单模块
Option Explicit
Sub example()
Dim cinv As CInventory
Set cinv = New CInventory
cinv.SPath = "H:\Joel\invent_2.prn"
cinv.AllocateInventoryData
Dim row, column As Integer
row = cinv.GetUBoundInventoryDataRows
column = cinv.GetUBoundInventoryDataColumns
Dim Destination As Range
Set Destination = Range("a1")
Destination.Resize(column, row).Value = Application.Transpose(cinv.GetInventoryData)
Set cinv = Nothing
End Sub