如何循环浏览工作表的子集?
我知道如何循环遍历工作簿中的所有工作表,以及如何在到达“结束标志”工作表时退出:
For Each ThisWorkSheet In Worksheets
If ThisWorkSheet.Name = "FlagEnd" Then Exit For
MsgBox "This worksheet name is: " & ThisWorkSheet.Name
Next
但是,我无法让循环在“开始标志”工作表上开始(或者更好地在 标记的开始/结束工作表位于一堆其他工作表的中间,因此开始或结束遍历是不可行的。
例如, 所以我真的需要从正确的工作表开始
尝试:
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
I know how to loop through all the worksheets in a workbook, and how to exit once I reach an 'end-flag' worksheet:
For Each ThisWorkSheet In Worksheets
If ThisWorkSheet.Name = "FlagEnd" Then Exit For
MsgBox "This worksheet name is: " & ThisWorkSheet.Name
Next
However I cannot get the loop to begin on a 'start-flag' worksheet (or even better on the worksheet right after the start-flag worksheet. For example the flagged start/end worksheets are in the middle of a bunch of other worksheets, so beginning or end traversing is not workable.
There could be hundreds of worksheets before that 'FlagStart' sheet, so I really need to start on the right sheet.
Tried:
Set ThisWorkSheet = Sheets("FlagNew")
and
For Each Sheets("FlagNew") In Worksheets
Ideas?
Solution:
Mathias was very close, but dendarii was that tiny step closer with the custom ending index. I actually figured out my final solution on my own, but wanted to give credit. Here was my final solution:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果这不是一个特别容易更改的工作簿(即不会一直添加和删除工作表),您可以将工作表的名称存储在隐藏工作表的某个范围内,并按名称循环遍历它们。
然而,听起来它们是连续存储在工作簿中的,因此,在 Mathias 的解决方案的基础上,您可以使用一个函数返回开始和结束工作表的索引,然后循环:
If this is not a particularly changeable workbook (i.e. worksheets are not being added and deleted all the time), you could store the names of the worksheets in a range on a hidden sheet and loop through them by name.
However, it sounds like they are stored consecutively in the workbook so, building on Mathias' solution, you could use a function to return the indices of the start and end worksheets and then loop through:
我相信,如果您使用“foreach”,您将无法控制起始表。就此而言,我什至不确定您能否保证迭代发生的顺序。
我认为你应该做的是首先获取你感兴趣的工作表的索引(按名称获取工作表,并获取其索引),然后使用 for 循环迭代从标志开始的工作表的索引工作表索引。
[编辑:我通过一个简单的例子进行了破解]
I believe that if you use "foreach" you won't have any control over the starting sheet. For that matter, I am not even sure you are guaranteed the order in which the iteration will take place.
I think what you should do is first, get the index of the sheet you are interested in (get the sheet by name, and get its index), and then iterate using a for loop, over the indexes of the sheets starting at the flag sheet index.
[Edit: I hacked through a quick example]
怎么样?
这段代码可能不太正确。我是用 SO 编辑器而不是 VBA 编写它,但你明白了。
How about?
This code might not be quite right. I'm writing it in the SO editor not VBA, but you get the idea.
您迭代的工作表是否具有通用的名称格式?
例如)
如果是这样,在 foreach 循环中,只需执行
Left(ThisWorkSheet.name, 4) = "emp"
即可验证它是否是您要引用的工作表。Do the sheets you iterate over have a common name format?
Ex)
If so, in your for each loop, just do a
Left(ThisWorkSheet.name, 4) = "emp"
to verify if it's a sheet you want to reference.在 Excel VBA 2013 中,如果您想要在“Blankfirst”和“Blanklast”选项卡之间更新工作表,则此方法有效。
使用下面的代码来测试它是否会返回您的选项卡名称,然后替换您的操作代码来代替
MsgBox wks.Name
部分。In Excel VBA 2013 if you have the worksheets you want to update between tabs "Blankfirst" and "Blanklast" this works.
Use the code below to test it brings back your tab names and then replace your manipulating code in place of
MsgBox wks.Name
part.