我是VBA的新手,并且一直在开发一个涉及数组的宏。
在数组部分,我需要将两个excel工作簿中的一些范围复制到两个单独的数组(timearray和guzikarray)。然后我将尝试匹配两个数组并将结果复制到第三个数组(masterarray)。最后,我将第三个数组的内容写回工作表。
但是当我开始进行匹配时,我发现我的前两个数组的内容会被自动删除。所以没有任何内容写回我的工作表。任何人都可以告诉我为什么?
'copy ranges to two arrays: timearray and guzikarray
masterrows = mastersheet.UsedRange.Rows.count
guzikrows = guziksheet.UsedRange.Rows.count
ReDim timearray(1 To masterrows, 1 To 2)
For i = 1 To i = masterrows
timearray(i, 1) = Cells(i, 6).Value
timearray(i, 2) = Cells(i, 10).Value
Next
ReDim guzikarray(1 To guzikrows, 1 To 6)
For i = 1 To i = guzikrows
guzikarray(i, 1) = guziksheet.Cells(i, 11).Value
guzikarray(i, 2) = guziksheet.Cells(i, 17).Value
guzikarray(i, 3) = guziksheet.Cells(i, 14).Value
guzikarray(i, 4) = guziksheet.Cells(i, 16).Value
guzikarray(i, 5) = guziksheet.Cells(i, 18).Value
guzikarray(i, 6) = guziksheet.Cells(i, 26).Value
Next
'match timearray and guzikarray, and then copied the results to masterarray
ReDim Preserve masterarray(1 To masterrows, 1 To 4)
For i = 2 To i = masterrows
min = 100000
For j = 2 To j = guzikrows
If timearray(i, 1) = guzikarray(j, 1) Then
If timearray(i, 2) = guzikarray(j, 2) Then
If guzikarray(j, 6) <> 0 Then
masterarray(i, 1) = guzikarray(j, 4)
masterarray(i, 3) = guzikarray(j, 3)
If guzikarray(j, 5) < min Then
min = guzikarray(j, 5)
masterarray(i, 2) = min
End If
If timearray(i, 1) <> timearray(i + 1, 1) Then
If timearray(i, 1) <> timearray(i - 1, 1) Then
If guzikarray(j, 2) <> guzikarray(j - 1, 2) Then masterarray(i, 4) = guzikarray(j - 1, 5)
End If
End If
End If
End If
End If
Next
Next
'write the results back to master array
Range(Cells(2, 17), Cells(masterrows, 20)).Value = masterarray
答案 0 :(得分:0)
由于语法不正确,您的循环无法正常工作
For i = 1 To i = masterrows
立即退出而不填充数组。你应该使用
For i = 1 To masterrows
For i = 1 To guzikrows
等