Private Sub Command1_Click()
Dim hFile As Integer
Dim sLine As String
Dim aFields() As String
Dim i, n As Integer
Dim aNums() As String
Dim tmpstr As Variant
Dim beginRead As Boolean
Dim aRst() As String
i = -1
beginRead = False
hFile = FreeFile
Open App.Path & "\3DBME.OUT" For Input Access Read As #hFile
While Not EOF(hFile)
Line Input #hFile, sLine
If i > 0 Then
i = i + 1
End If
If InStr(1, sLine, "MAXIMUM BASE SHEARS") > 0 Then
i = 1
End If
If (i = 3) Then
beginRead = True
End If
If beginRead Then '
aFields = Split(sLine, " ")
n = 0
For Each tmpstr In aFields
If Trim(tmpstr) <> "" Then
n = n + 1
ReDim Preserve aNums(n)
aNums(n - 1) = Trim(tmpstr)
End If
Next
ReDim Preserve aNums(n - 1)
MsgBox ("FORCE X" & aNums(1))
End If
Wend
Close #hFile
End Sub
我在运行上面的代码时候,已经MsgBox出我要找的东西,可按确定以后总提示我下标越界,按调试的话会指到ReDim Preserve aNums(n - 1),哪位高手给指点一下到底是哪出问题了
Dim hFile As Integer
Dim sLine As String
Dim aFields() As String
Dim i, n As Integer
Dim aNums() As String
Dim tmpstr As Variant
Dim beginRead As Boolean
Dim aRst() As String
i = -1
beginRead = False
hFile = FreeFile
Open App.Path & "\3DBME.OUT" For Input Access Read As #hFile
While Not EOF(hFile)
Line Input #hFile, sLine
If i > 0 Then
i = i + 1
End If
If InStr(1, sLine, "MAXIMUM BASE SHEARS") > 0 Then
i = 1
End If
If (i = 3) Then
beginRead = True
End If
If beginRead Then '
aFields = Split(sLine, " ")
n = 0
For Each tmpstr In aFields
If Trim(tmpstr) <> "" Then
n = n + 1
ReDim Preserve aNums(n)
aNums(n - 1) = Trim(tmpstr)
End If
Next
ReDim Preserve aNums(n - 1)
MsgBox ("FORCE X" & aNums(1))
End If
Wend
Close #hFile
End Sub
我在运行上面的代码时候,已经MsgBox出我要找的东西,可按确定以后总提示我下标越界,按调试的话会指到ReDim Preserve aNums(n - 1),哪位高手给指点一下到底是哪出问题了
MsgBox ("FORCE X" & aNums(1))以后循环继续
所以这个判断里面的代码没有进去
If Trim(tmpstr) <> "" Then
然后
ReDim Preserve aNums(n - 1)
这时 n=0 redim下标 -1出错