我知道如何遍历工作簿中的所有工作表,以及在到达“结束标志”工作表后如何退出:
For Each ThisWorkSheet In Worksheets
If ThisWorkSheet.Name = "FlagEnd" Then Exit For
MsgBox "This worksheet name is: " & ThisWorkSheet.Name
Next
但是,我无法在“开始标志”工作表上开始循环(甚至在开始标志工作表之后的工作表上更好。例如,标记的开始/结束工作表位于一堆其他工作表的中间,所以开始或结束遍历是不可行的。
在“FlagStart”工作表之前可能有数百个工作表,所以我真的需要从正确的工作表开始。
试过:
Set ThisWorkSheet = Sheets("FlagNew")
和
For Each Sheets("FlagNew") In Worksheets
想法?
解决方案:Mathias 非常接近,但 dendarii 与自定义结束索引更接近。实际上,我自己想出了我的最终解决方案,但想表扬一下。这是我的最终解决方案:
Private Sub CommandButtonLoopThruFlaggedSheets_Click()
' determine current bounds
Dim StartIndex, EndIndex, LoopIndex As Integer
StartIndex = Sheets("FlagNew").Index + 1
EndIndex = Sheets("FlagEnd").Index - 1
For LoopIndex = StartIndex To EndIndex
MsgBox "this worksheet is: " & Sheets(LoopIndex).Name
' code here
Next LoopIndex
End Sub