1

我试图弄清楚如何在For循环中跳过迭代。我做了一些研究,发现我可以使用Continue For,但这并不能解决我的问题。这是我想做的一个例子:

For i As Long = 1 to 7 Step 1
    If (i= 2, 5 and 7) Then
        'perform this action
    Else
        'perform other action.
    End If
Next i

我制定了以下内容,但不幸的是,它适用于我的循环的 and 部分和<= 2and ,执行与我要求在该部分执行的操作相同的操作。Else57Else

For i As Long = 1 To 7 Step 1
    If (i <= 2 AndAlso 5 AndAlso 7) Then
        strRange = ("A:D")
    Else
        strRange = ("A:A")
    End If

    xlRefSheets = ClientSheets(i)

    With xlRefSheets
        .Cells.EntireColumn.AutoFit()
        .Range(strRange).EntireColumn.Hidden = True
    End With
Next i
4

1 回答 1

2

如何将if条款重述为

If (i <= 2) or (i = 5) or ( i =7)  Then
   ...

所以你的代码变成:

  For i As Long = 1 To 7 Step 1
    If (i <= 2) OR (i = 5) OR (i = 7) Then
        strRange = ("A:D")
    Else
        strRange = ("A:A")
    End If

    xlRefSheets = ClientSheets(i)
    With xlRefSheets
        .Cells.EntireColumn.AutoFit()
        .Range(strRange).EntireColumn.Hidden = True
    End With
  Next i
于 2013-09-26T22:20:31.647 回答