请教VB的这段代码,下标越界,看不出问题出在哪里?多谢了

来源:百度知道 编辑:UC知道 时间:2024/05/29 17:04:16
Private Sub Command1_Click()
Open "d:\23.txt" For Input As #1 '程序路径下的data.txt文件

Dim X(), Y() As String

MSFlexGrid1.Cols = 3
MSFlexGrid1.TextMatrix(0, 1) = "年份"
MSFlexGrid1.TextMatrix(0, 2) = "月均流量"
Do While Not EOF(1)
Line Input #1, tmp
If tmp <> "" Then
arr = Split(tmp, " ") '这里是以空格为分隔的,如果是逗号就用split(str,",")
If UBound(arr) > 0 Then
n = n + 1
ReDim Preserve Y(n - 1)
ReDim Preserve X(n - 1)
Y(n - 1) = arr(0)
X(n - 1) = arr(1)
MSFlexGrid1.TextMatrix(n, 1) = arr(0)
MSFlexGrid1.TextMatrix(n, 2) = arr(1)
MSFlexGrid1.AddItem ""
End If
End If
Loop
Close #1
End Sub
这是读取数据,储存在X()中的数据下面计算会用到,下面的计算代码:
Private Sub Command2_Click()
Dim R1() As String, R2() As Single

Dim X(), Y() As String
放到过程外面就行了

因为X()是在两个过程中用
所以要放在窗体的头部声明
这样才能在两个过程中共享
其他需要共享的代码同理。。。

============================
窗体代码

Option Explicit

Dim X(), Y() As String '窗体头部。。。

Private Sub Command1_Click()
Open "d:\23.txt" For Input As #1 '程序路径下的data.txt文件

'Dim X(), Y() As String '移动到窗体头部。。。