如何循环浏览工作表的子集?

发布于 2024-08-07 19:04:33 字数 997 浏览 10 评论 0原文

我知道如何循环遍历工作簿中的所有工作表,以及如何在到达“结束标志”工作表时退出:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

独享拥抱 2024-08-14 19:04:33

如果这不是一个特别容易更改的工作簿(即不会一直添加和删除工作表),您可以将工作表的名称存储在隐藏工作表的某个范围内,并按名称循环遍历它们。

然而,听起来它们是连续存储在工作簿中的,因此,在 Mathias 的解决方案的基础上,您可以使用一个函数返回开始和结束工作表的索引,然后循环:

Public Function GetStartIndex() As Integer
    On Error Resume Next
    GetStartIndex = ThisWorkbook.Worksheets("MyStartingWorksheet").Index + 1
End Function

Public Function GetEndIndex() As Integer
    On Error Resume Next
    GetEndIndex = ThisWorkbook.Worksheets("MyEndingWorksheet").Index - 1
End Function

Sub LoopThrough()

    Dim wks As Worksheet
    Dim i As Integer
    Dim iStart As Integer
    Dim iEnd As Integer

    iStart = GetStartIndex()
    iEnd = GetEndIndex()

    If iStart > 0 And iEnd > 0 And iEnd > iStart Then
        For i = iStart To iEnd
            Set wks = ThisWorkbook.Worksheets(i)
            MsgBox wks.Name
        Next i
    End If

End Sub

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:

Public Function GetStartIndex() As Integer
    On Error Resume Next
    GetStartIndex = ThisWorkbook.Worksheets("MyStartingWorksheet").Index + 1
End Function

Public Function GetEndIndex() As Integer
    On Error Resume Next
    GetEndIndex = ThisWorkbook.Worksheets("MyEndingWorksheet").Index - 1
End Function

Sub LoopThrough()

    Dim wks As Worksheet
    Dim i As Integer
    Dim iStart As Integer
    Dim iEnd As Integer

    iStart = GetStartIndex()
    iEnd = GetEndIndex()

    If iStart > 0 And iEnd > 0 And iEnd > iStart Then
        For i = iStart To iEnd
            Set wks = ThisWorkbook.Worksheets(i)
            MsgBox wks.Name
        Next i
    End If

End Sub
不乱于心 2024-08-14 19:04:33

我相信,如果您使用“foreach”,您将无法控制起始表。就此而言,我什至不确定您能否保证迭代发生的顺序。
我认为你应该做的是首先获取你感兴趣的工作表的索引(按名称获取工作表,并获取其索引),然后使用 for 循环迭代从标志开始的工作表的索引工作表索引。
[编辑:我通过一个简单的例子进行了破解]

Sub Iterate()

Dim book As Workbook
Dim flagIndex As Integer
Dim flagSheet As Worksheet

Set book = ActiveWorkbook
Set flagSheet = book.Worksheets("Sheet3")
flagIndex = flagSheet.Index

Dim sheetIndex As Integer
Dim currentSheet As Worksheet

For sheetIndex = flagIndex To book.Worksheets.Count
    Set currentSheet = book.Worksheets(sheetIndex)
Next

End Sub

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]

Sub Iterate()

Dim book As Workbook
Dim flagIndex As Integer
Dim flagSheet As Worksheet

Set book = ActiveWorkbook
Set flagSheet = book.Worksheets("Sheet3")
flagIndex = flagSheet.Index

Dim sheetIndex As Integer
Dim currentSheet As Worksheet

For sheetIndex = flagIndex To book.Worksheets.Count
    Set currentSheet = book.Worksheets(sheetIndex)
Next

End Sub
过期情话 2024-08-14 19:04:33

怎么样?

For Each ThisWorkSheet In Worksheets  
    If ThisWorkSheet.Name = "FlagStart" Then output = true 
    If ThisWorkSheet.Name = "FlagEnd" Then Exit For   
    If output = true Then MsgBox "This worksheet name is: " & ThisWorkSheet.Name
Next

这段代码可能不太正确。我是用 SO 编辑器而不是 VBA 编写它,但你明白了。

How about?

For Each ThisWorkSheet In Worksheets  
    If ThisWorkSheet.Name = "FlagStart" Then output = true 
    If ThisWorkSheet.Name = "FlagEnd" Then Exit For   
    If output = true Then MsgBox "This worksheet name is: " & ThisWorkSheet.Name
Next

This code might not be quite right. I'm writing it in the SO editor not VBA, but you get the idea.

无言温柔 2024-08-14 19:04:33

您迭代的工作表是否具有通用的名称格式?

例如)

Sheets(0).name > "Reports"
Sheets(1).name > "Start Here"
Sheets(2).name > "emp.0001"
Sheets(3).name > "emp.0002"
Sheets(4).name > "emp.0003"
Sheets(5).name > "emp.0004"
Sheets(6).name > "End Here"

如果是这样,在 foreach 循环中,只需执行 Left(ThisWorkSheet.name, 4) = "emp" 即可验证它是否是您要引用的工作表。

Do the sheets you iterate over have a common name format?

Ex)

Sheets(0).name > "Reports"
Sheets(1).name > "Start Here"
Sheets(2).name > "emp.0001"
Sheets(3).name > "emp.0002"
Sheets(4).name > "emp.0003"
Sheets(5).name > "emp.0004"
Sheets(6).name > "End Here"

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.

夜雨飘雪 2024-08-14 19:04:33

在 Excel VBA 2013 中,如果您想要在“Blankfirst”和“Blanklast”选项卡之间更新工作表,则此方法有效。

使用下面的代码来测试它是否会返回您的选项卡名称,然后替换您的操作代码来代替 MsgBox wks.Name 部分。

Sub Macro2()

    On Error Resume Next
    GetStartIndex = ThisWorkbook.Worksheets("Blankfirst").Index + 1

    On Error Resume Next
    GetEndIndex = ThisWorkbook.Worksheets("Blanklast").Index - 1

    Dim wks As Worksheet
    Dim i As Integer
    Dim iStart As Integer
    Dim iEnd As Integer

    iStart = GetStartIndex
    iEnd = GetEndIndex


    If iStart > 0 And iEnd > 0 And iEnd > iStart Then
        For i = iStart To iEnd
            Set wks = ThisWorkbook.Worksheets(i)
            MsgBox wks.Name
        Next i
    End If

End Sub

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.

Sub Macro2()

    On Error Resume Next
    GetStartIndex = ThisWorkbook.Worksheets("Blankfirst").Index + 1

    On Error Resume Next
    GetEndIndex = ThisWorkbook.Worksheets("Blanklast").Index - 1

    Dim wks As Worksheet
    Dim i As Integer
    Dim iStart As Integer
    Dim iEnd As Integer

    iStart = GetStartIndex
    iEnd = GetEndIndex


    If iStart > 0 And iEnd > 0 And iEnd > iStart Then
        For i = iStart To iEnd
            Set wks = ThisWorkbook.Worksheets(i)
            MsgBox wks.Name
        Next i
    End If

End Sub
月下客 2024-08-14 19:04:33
 Public Sub ITERATE_WORKSHEETS()  
   On Error Resume Next    
   Dim x As Long  
         For x = 0 To 100  
              MsgBox Worksheets(x).Name  
         Next x   
   On Error GoTo 0  
   MsgBox "all done"  
 End Sub  
 Public Sub ITERATE_WORKSHEETS()  
   On Error Resume Next    
   Dim x As Long  
         For x = 0 To 100  
              MsgBox Worksheets(x).Name  
         Next x   
   On Error GoTo 0  
   MsgBox "all done"  
 End Sub  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文