txt文件存入到2维数组中出错问题
下面有一段VB代码,我想把txt文件存入到2维数组中去,但是怎么执行就是出错,希望有高手能给看看
Dim shuju() As Double
Dim shujuhangshu As Integer
Const fengefu = ","
Const meihangshujushu = 7
Const shujuwenjian = "C:\Documents and Settings\Administrator\ラタテ觸mess\biao1.txt"
Private Sub Command1_Click()
On Error GoTo wenjiancuo
Dim a As Variant
Open shujuwenjian For Input As #1
shujushu = 0
Do Until EOF(1)
Line Input #1, yihangshuju
ReDim Preserve shuju(shujuhangshu, meihangshujushu)
a = Split(yihangshuju, fengefu)
If UBound(a) > meihangshujushu Then GoTo zhujushucuo
For i = 0 To UBound(a)
shuju(shujuhangshu, i) = a(i)
Next i
shujuhangshu = shujuhangshu + 1
Loop
Close #1
MsgBox "指定文件已经顺利读入数组!"
Command1.Enabled = False
Exit Sub
wenjiancuo:
MsgBox "你指定的文件文件不能打开!"
Exit Sub
shujushucuo:
Close #1
MsgBox "你指定的每行数据数少于文件里第" & shujuhangshu & "行的数据数目!"
End Sub
Private Sub Form_Load()
Command1.Caption = "读取数据"
End Sub
[解决办法]
ReDim Preserve shuju(shujuhangshu, meihangshujushu)
shujuhangshu = shujuhangshu + 1
在用 Preserve 关键字时,只能改变多维数组中最后一维的上界;如果改变了其它维或最后一维的下界,那么运行时就会出错
[解决办法]
zhujushucuo应为shujushucuo,有错就跳转,再仔细检查还有没错
[解决办法]
shuju(i, shujuhangshu) = a(i)