如何在for循环中偏移范围

时间:2019-02-24 17:28:56

标签: vba for-loop

我想为rng1中的每个单元格偏移rng2

现在在我的Rng2中,Q默认设置为零

每次我循环遍历新单元格时(我的第一个for循环),我希望Q增加1,因为对于Rng中的每个单元格,我都希望在新行上进行匹配。

我在代码中将Q = Q + 1放在哪里 我的其余代码工作正常,每次我循环通过rng1中的新单元格时,我只想将rng2偏移一行。任何帮助将不胜感激

    Sub StructurePivot2()
    Dim X As Variant, Var As Double, D As Integer, P As Integer, Q As Integer, Z As Integer, ws As Worksheet, rng3 As Range, category As Variant, rng As Range, cell As Variant, i As Integer, rng2 As Range
    Set ws = Worksheets("Sheet1")
    Set rng = Worksheets("Sheet1").Range("A2:A173")
    Set rng2 = Worksheets("Sheet1").Range("B2:AI2").Offset(Q)
    P = 0
        For Each cell In rng

                        For i = 1 To 5

                          Z = Application.WorksheetFunction.Match(i, rng2, 0)
                          Worksheets("Sheet2").Range("A3").Offset(P, 13) = Z
                          P = P + 1

                     Next i

Next cell
End Sub

1 个答案:

答案 0 :(得分:0)

在循环中:

Next i
set rng2 = rng2.Offset(1, 0) 'Advance to next row
Next cell