1

I am a newbie to VBA and have been developing a macro that involves arrays.

In the array part, I need to copy some ranges from two excel workbooks into two separate arrays(timearray and guzikarray). Then I will try to match the two arrays and copied the results into a third array(masterarray). Lastly I will write the third array's contents back into the worksheet.

But I found that my first two arrays' contents are automatically erased when I started to do the matching. So nothing was written back to my worksheet. Anyone can tell me why?

'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
4

1 回答 1

0

由于语法不正确,您的循环不起作用

For i = 1 To i = masterrows
立即退出而不填充数组。你应该使用

For i = 1 To masterrows
For i = 1 To guzikrows ETC

于 2013-06-26T06:56:33.880 回答