我需要帮助控制嵌套的If ... then语句中的循环重复。对于i = 5和i = 6,我如何确保操作只发生一次?
For i = 5 To 26
Set sht = wkbk.Sheets(i)
sht.Cells(3, 1).Value = "'" & StrConv(inputbx, vbProperCase) & " 2017"
If i = 5 Or 6 Then
Cells(7, 6).End(xlToRight).EntireColumn.Select
Selection.AutoFill Destination:=Range(Selection, Selection.Offset(0, 1))
End If
Next i
答案 0 :(得分:1)
更改if并确保您引用正确的工作表:
Dim clm As Long
For i = 5 To 26
With wkbk.Sheets(i)
.Cells(3, 1).Value = "'" & StrConv(inputbx, vbProperCase) & " 2017"
If i = 5 Or i = 6 Then
clm = .Cells(7, 6).End(xlToRight).Column
.Columns(clm).AutoFill Destination:=.Range(.Columns(clm), .Columns(clm).Offset(0, 1))
End If
End With
Next i
答案 1 :(得分:0)
并添加一个新变量,如果你想要嵌套,如果只发生一次5和6:
Dim to_work = true
For i = 5 To 26
Set sht = wkbk.Sheets(i)
sht.Cells(3, 1).Value = "'" & StrConv(inputbx, vbProperCase) & " 2017"
If (i = 5 Or i = 6) and to_work Then
sht.Cells(7, 6).End(xlToRight).EntireColumn.Select
Selection.AutoFill Destination:=Range(Selection, Selection.Offset(0, 1))
to_work = false
End If
Next i