VBA - 动态范围跳过行

VBA - 动态范围跳过行
For X = 9 To 36

For Y = 8 To 22 Step 14

If WS1.Cells(X, 1) = "TOT" Then X = X + 2
    
Buy= WorksheetFunction.SumIfs(Calls, Hour, WS1.Cells(X, 1), Data, WS1.Cells(Y, 2), Service, Number) + WorksheetFunction.SumIfs(Calls, Hour, WS1.Cells(X, 1), Data, WS1.Cells(Y, 2), Service, Number2)

WS1.Cells(X, 2) = Buy

这应该使它像 X = 8 Y = 8,X = 9 Y = 8,X = 10 Y = 8 等直到 21 它“跳”到 23 并且 Y 应该做同样的事情并转到 22 所以基本上 Y 必须跳 14(或其 X - 1)但由于某种原因它不起作用

答案1

您可以执行一个完整的循环,并在循环内部跳过其迭代。

例如:

Counter = 0
For X = 9 To 999
    Counter = Counter +1
    if Counter > 11 then
        'we skip these...
        if Counter > 14 then
            'Reset counter
            Counter = 0
        End if
    else

        Y = 6
            
        Buy= WorksheetFunction.SumIfs(Calls, Hour, WS1.Cells(X, 1), Data, WS1.Cells(Y, 2), Service, Number) + WorksheetFunction.SumIfs(Calls, Hour, WS1.Cells(X, 1), Data, WS1.Cells(Y, 2), Service, Number2)
    
        WS1.Cells(X, 2) = Buy

    end if

Next X

相关内容