Excel VBA中的双精度循环,不同的下标

Excel VBA中的双精度循环,不同的下标,excel,vba,loops,double,subscript,Excel,Vba,Loops,Double,Subscript,我对VBA中的嵌套循环有问题。我需要执行的算法如下所示: Option base 1 arr(4,2) i = 1 To 4 j = 2 To 100 i = 1, j = 2 If Cells(2, 1).Value = arr(1,1) Then Cells(2,11) = arr(1,2) i = 1, j = 3 If Cells(3,1).Value = arr(1,1) Then Cells(3,11) = arr(1

我对VBA中的嵌套循环有问题。我需要执行的算法如下所示:

Option base 1
arr(4,2)
i = 1 To 4
j = 2 To 100

i = 1, j = 2 If Cells(2, 1).Value = arr(1,1) Then
                Cells(2,11) = arr(1,2)
i = 1, j = 3 If Cells(3,1).Value = arr(1,1) Then
                Cells(3,11) = arr(1,2)
i = 1, j = 4 If Cells(4,1).Value = arr(1,1) Then
                Cells(4,11).Value = arr(1,2) 
.
.
.

i = 1, j = 100 If Cells(100,1).Value = arr(1,1) Then
                  Cells(100,11).Value = arr(1,2)

And if j reaches 100 then

i = 2, j = 2 If Cells(2,1).Value = arr(2,1) Then
                Cells(2,11).Value = arr(2,2)
i = 2, j = 3 If Cells(3,1).Value = arr(2,1) Then
                Cells(3,11).Value = arr(2,2)
i = 2, j = 4 If Cells(4,1).Value = arr(2,1) Then
                Cells(4,11).Value = arr(2,2)
.
.
.

i = 2, j = 100 If Cells(100,1).Value = arr(2,1) Then
                  Cells(100,1).Value = arr(2,2)
依此类推,直到i=4。 我希望您能理解:D现在我需要在VBA中执行此操作-现在我不知道如何执行,我所做的一切都失败了,很不幸,所以我甚至没有代码示例给您;/我试过这样的方法

For i = 1 To 4
    For j = 1 To 100
      If Cells(j, 1).Value = arr(i, 1) Then
         Cells(j,11).Value = arr(i,2)
      End If
    Next j
Next i
当然,它显示的下标超出了范围,我也尝试了针对每个下标的一些方法,但同样的问题


请提供帮助:D

根据您的上述尝试,您需要一个工作表对象来使用.Cells。见下文:

For i = 1 To 4
    For j = 1 To 100
        If Sheets("mySheet").Cells(j, 1).Value = arr(i, 1) Then
            Sheets("mySheet").Cells(j, 11).Value = arr(i, 2)
        End If
    Next j
Next i
如果仍然遇到下标超出范围,请查看数组维度,因为您可能需要声明它以考虑arr4,1和arr4,2

有关.Cells属性的详细信息,请参见此处:


谢谢,尽管我的错误是不同的错误,但我的上界是错的…没关系,你的回答让我确信我的想法基本上是正确的。再次感谢!