主题:急,急,下标越界问题,请教!!大家帮忙看看
下面代码中,下划线那句应在循环内还是外面?红色这句提示下标越界,请高手指点.!!!
请大家看看
Private Sub Command4_Click()
Dim i As Integer, j As Integer, k As Integer, s As Integer '定义i,j,k为整型变量
Dim daynum As Integer '定义每月的天数
Dim stationname As String
Dim stationnum As Integer
Const station = 10, group = 3 '定义总台站数,及雨量组数
Dim result()
Dim arain()
Dim monthnum, yearnum '定义为变体variant变量
..................................................
...............................................
For X = 1 To Len(CommonDialog1.FileName)
'从文件名FileNames(X)(不带路径的)中取年份、月份及stationname站名
stationname = Mid(FileNames(X), 2, 5)
yearnum = Mid(FileNames(X), 8, 4)
monthnum = Mid(FileNames(X), 12, 2)
'取该月的天数
Select Case monthnum
........................
End Select
'由站名确定列数
Select Case stationname
.......................................
End Select
FileNames(X) = FileNames(0) & FileNames(X) '文件名加路径
'分别打开各个文本文件,FileNames(X)带路径
Dim sA As String
Dim R As Long
Open "FileNames(X)" For Binary As #1
sA = Space(LOF(1)) '用空格填充sA变量
Get #1, , sA '用Get语句获取文件全部内容
R = InStr(1, sA, "R6") '在sA中查找R6的位置
'读文本arain()的值
[color=FF0000]ReDim arain(1 To s, 1 To k)[/color]
s = 1: k = 1
Do While s <= daynum
Do While k <= group
arain(s, k) = Mid(sA, R + 3 + 5 * (k - 1) + 1 * (s - 1), 4)
k = k + 1
Loop
k = k - 3
s = s + 1 'A文件中雨量的行数
Loop
Close #1 '关闭文件
[u] ReDim result(1 To i, 1 To j) [/u] '为动态数组分配实际的元素数
'数组赋值,实现循环赋值至结果
i = 1: j = stationnum + 1: s = 1: k = 1
Do While s <= daynum
Do While k < group
result(i, j) = arain(s, k)
i = i + 1
k = k + 1
Loop
k = k - 2
s = s + 1
Loop
Next X
'写入文件输出result数组
Dim n
For n = 1 To daynum
result(2 * n - 1, 1) = n & "08"
result(2 * n, 1) = n & "20"
Next n '为result数组增加时次列
'将数组写入字符串
Dim rs As String
rs = "" '字符串 初值设为空
For i = 1 To 2 * daynum
For j = 1 To station + 1
rs = rs + result(i, j) + Chr(32) '空格
Next j
rs = rs & vbCrLf '回车换行
Next i
'在当前路径新建一个以文本框内容命名的文件,并将字符串写入
Open App.Path & "\" & Text1.Text & ".txt" For Output As #1
Print #1, " 扶沟 太康 鹿邑 西华 淮阳 郸城 周口 沈丘 项城 商水"
Print #1, , rs '格式对否???
Close #1
End Sub
请大家看看
Private Sub Command4_Click()
Dim i As Integer, j As Integer, k As Integer, s As Integer '定义i,j,k为整型变量
Dim daynum As Integer '定义每月的天数
Dim stationname As String
Dim stationnum As Integer
Const station = 10, group = 3 '定义总台站数,及雨量组数
Dim result()
Dim arain()
Dim monthnum, yearnum '定义为变体variant变量
..................................................
...............................................
For X = 1 To Len(CommonDialog1.FileName)
'从文件名FileNames(X)(不带路径的)中取年份、月份及stationname站名
stationname = Mid(FileNames(X), 2, 5)
yearnum = Mid(FileNames(X), 8, 4)
monthnum = Mid(FileNames(X), 12, 2)
'取该月的天数
Select Case monthnum
........................
End Select
'由站名确定列数
Select Case stationname
.......................................
End Select
FileNames(X) = FileNames(0) & FileNames(X) '文件名加路径
'分别打开各个文本文件,FileNames(X)带路径
Dim sA As String
Dim R As Long
Open "FileNames(X)" For Binary As #1
sA = Space(LOF(1)) '用空格填充sA变量
Get #1, , sA '用Get语句获取文件全部内容
R = InStr(1, sA, "R6") '在sA中查找R6的位置
'读文本arain()的值
[color=FF0000]ReDim arain(1 To s, 1 To k)[/color]
s = 1: k = 1
Do While s <= daynum
Do While k <= group
arain(s, k) = Mid(sA, R + 3 + 5 * (k - 1) + 1 * (s - 1), 4)
k = k + 1
Loop
k = k - 3
s = s + 1 'A文件中雨量的行数
Loop
Close #1 '关闭文件
[u] ReDim result(1 To i, 1 To j) [/u] '为动态数组分配实际的元素数
'数组赋值,实现循环赋值至结果
i = 1: j = stationnum + 1: s = 1: k = 1
Do While s <= daynum
Do While k < group
result(i, j) = arain(s, k)
i = i + 1
k = k + 1
Loop
k = k - 2
s = s + 1
Loop
Next X
'写入文件输出result数组
Dim n
For n = 1 To daynum
result(2 * n - 1, 1) = n & "08"
result(2 * n, 1) = n & "20"
Next n '为result数组增加时次列
'将数组写入字符串
Dim rs As String
rs = "" '字符串 初值设为空
For i = 1 To 2 * daynum
For j = 1 To station + 1
rs = rs + result(i, j) + Chr(32) '空格
Next j
rs = rs & vbCrLf '回车换行
Next i
'在当前路径新建一个以文本框内容命名的文件,并将字符串写入
Open App.Path & "\" & Text1.Text & ".txt" For Output As #1
Print #1, " 扶沟 太康 鹿邑 西华 淮阳 郸城 周口 沈丘 项城 商水"
Print #1, , rs '格式对否???
Close #1
End Sub