请教,找了半天都没找到问题出在那
我用FSO组件读文本文件中数据到二纬数组中,各位帮我看看呗,问题出在那了
或是有什么更好的办法吗,谢谢了
代码如下
Dim mFSO As New Scripting.FileSystemObject
Dim TRM As Scripting.TextStream
Dim mFile As String
If Right(File1.Path, 1) <> "\ " Then
mFile = File1.Path + "\ " + File1.FileName
Else
mFile = File1.Path + File1.FileName
End If
Set TRM = mFSO.OpenTextFile(mFile)
Dim s As String
Dim s1 As String
s = TRM.ReadAll
Dim buff() As String
buff = Split(s, vbCrLf)
Dim n As Long, m As Long, i As Long ', e As Long
n = UBound(buff)
For i = 0 To UBound(buff) - 1
s = buff(i)
s = fun(s)
buff(i) = s
Next i
Dim buff1()
ReDim buff1(n)
For i = 0 To UBound(buff) - 1
buff1(i) = Split(buff(i), " ")
Next i
Private Function fun(ByVal s As String) As String
Do While (InStr(s, " ") <> 0)
s = Replace(s, " ", " ")
Loop
fun = s
End Function
[解决办法]
你什么时候定义二维数组了?
下半段如下修改:
Dim k As Long
n = UBound(buff)
For i = 0 To UBound(buff) - 1
s = buff(i)
s = fun(s)
k = UBound(Split(s, " "))
If k > m Then m = k 'm = max(m,k)
buff(i) = s
Next i
Dim buff1()
ReDim buff1(n, m)
Dim j As Long
Dim a As Variant
For i = 0 To UBound(buff) - 1
a = Split(buff(i), " ")
For j = 0 To UBound(a)
buff1(i, j) = a(j)
Next
Next i
[解决办法]
Dim buff1()
ReDim buff1(n)
For i = 0 To UBound(buff) - 1
buff1(i) = Split(buff(i), " ")
Next i
实际上这一段本身并没有多大问题,就是ubound(buff)-1应改为n
但是,你要引用buff1时,就得这样引用了:
Debug.Print buff1(0)(0), buff1(0)(1)
而不能 buff1(0,0),buff1(0,1)