循环浏览所有工作表VBA

循环浏览所有工作表VBA,vba,excel,Vba,Excel,我试图循环浏览activeworkbook中的所有工作表以执行重复任务 我目前拥有以下代码: Sub sort_sectors() Dim i As Integer Dim rng As Range Dim SortRng As Range Dim rng1 As Integer Dim ws As Worksheet Dim wb As Workbook Dim LastCol As Long Dim LastRow As Long Set wb = ActiveWorkbook For

我试图循环浏览activeworkbook中的所有工作表以执行重复任务

我目前拥有以下代码:

Sub sort_sectors()

Dim i As Integer
Dim rng As Range
Dim SortRng As Range
Dim rng1 As Integer
Dim ws As Worksheet
Dim wb As Workbook
Dim LastCol As Long
Dim LastRow As Long

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets

'This is marking several of the sheets of which I do not want to run the sub
If ws.Range("a9").Value = "x" Then
NextIteration:
End If

'Reference point is rng1 to select the desired range
With Range("a1:t100")
    rng1 = .Find(what:="sector", LookIn:=xlValues).Row
End With

'return the row number for the sector header
LastCol = ws.Cells(20, ws.Columns.Count).End(xlToLeft).Column
LastRow = ws.Range("a15").End(xlDown).Row

'I am going to add the code below to finish out the task that I want to complete

Next

End Sub
我确信问题在于我误解了for-each循环的实际工作方式。希望有人的回答能让你更好地理解

我真的很感谢你在这方面的帮助

我对代码做了一些编辑,现在我确实有一个错误:)我尝试对代码中的“with ws.range etc…”部分进行了您建议的更改,得到了对象错误91

下面是我新的“改进”代码

Sub-sort_扇区()
作为整数的Dim i
变暗rng As范围
模糊排序范围
Dim intAnchorRow作为整数
Dim intMktCapAnchor为整数
Dim intSectorAnchor作为整数
将ws设置为工作表
将wb设置为工作簿
暗淡如长
最后一排一样长
设置wb=ActiveWorkbook
对于ActiveWorkbook.Worksheets中的每个ws
'过滤掉我们不想运行的工作表
如果ws.Range(“a9”).Value“x”或ws.Name=“\uuuuu FDSCACHE\uuuuuu”或ws.Name=“INDEX”,则
'获取用于获取排序范围和排序键的定位点
这就是现在给我错误的部分
带ws.范围(“a1:t100”)
intAnchorRow=.Find(what:=“扇区”,LookIn:=xlValues)。行
intSectorAnchor=.Find(what:=“扇区”,LookIn:=xlValues)。列
intMktCapAnchor=.Find(what:=“市值”,LookIn:=xlValues)。列
以
'查找数据范围的最后一行和最后一列
LastCol=ws.Cells(20,ws.Columns.Count).End(xlToLeft).Column
LastRow=ws.Range(“a15”).End(xlDown).Row
设置SortRng=范围(单元格(intAnchorRow+1,1),单元格(LastRow,LastCol))
范围(SortRng).排序键1:=范围(单元格(intAnchorRow+1,intSectorAnchor),单元格(LastRow,intSectorAnchor))_
顺序1:=xl升序,键2:=范围(单元格(intAnchorRow+1,intmktcanchor),单元格(LastRow,intmktcanchor))_
订单2:=xl降序,标题:=xlNo
如果结束
下一个
端接头

再次感谢。这对我很有帮助

如果我正确理解了您的问题,您不希望使用单元格
A9
中带有
x
的工作表

如果是这种情况,我将更改
If
语句的条件,以检查单元格是否不包含
x
。如果这是真的,它将输入代码的其余部分。如果没有,则转到下一次迭代

另外,您的
NextIteration:
If
语句中没有任何作用

Sub sort_sectors()

Dim i As Integer
Dim rng As Range
Dim SortRng As Range
Dim rng1 As Integer
Dim ws As Worksheet
Dim wb As Workbook
Dim LastCol As Long
Dim LastRow As Long

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets

    'This is marking several of the sheets of which I do not want to run the sub
    If ws.Range("a9").Value <> "x" Then

        'Reference point is rng1 to select the desired range
        With Range("a1:t100")
            rng1 = .Find(what:="sector", LookIn:=xlValues).Row
        End With

        'return the row number for the sector header
        LastCol = ws.Cells(20, ws.Columns.Count).End(xlToLeft).Column
        LastRow = ws.Range("a15").End(xlDown).Row

        'I am going to add the code below to finish out the task that I want to complete

    End If
Next    
End Sub
当然,如果您愿意,您可以在代码中使用此结构,并在下一个

e、 g


我应该澄清一下。我一步一步地检查代码,却看不到任何事情发生。我甚至添加了一个ws.range(“a1”).value=“something”来检查。如果你有其他问题,你应该开始一个新问题,因为这个问题已经“解决”。这对我来说很有意义。谢谢你的帮助。当我一步一步地浏览代码时,似乎第一次通过循环时没有更新任何工作表。在循环的第二次运行中,我可以看到在工作簿的第一页上运行的代码。这是每个ws循环的一个怪癖,还是我在这里遗漏了什么?我没有注意到每个ws循环的
有任何怪癖;听起来好像还有别的事。您可能需要将
ws
包含在带范围的
中(“a1:t100”)
,以给出带范围的
(“a1:t100”)
@dutchballa让我知道是否是这种情况,我会更新我的答案。我不知道原因是什么,但我在新工作簿上尝试了该代码,并且没有相同的问题。我认为这不值得去想。谢谢你帮我解决这个问题。@dutchballa不用担心。我已经更新了我的答案,向您展示了如何使用
符号,如果您愿意的话。
Sub sort_sectors()

Dim i As Integer
Dim rng As Range
Dim SortRng As Range
Dim rng1 As Integer
Dim ws As Worksheet
Dim wb As Workbook
Dim LastCol As Long
Dim LastRow As Long

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets

    'This is marking several of the sheets of which I do not want to run the sub
    If ws.Range("a9").Value <> "x" Then

        'Reference point is rng1 to select the desired range
        With Range("a1:t100")
            rng1 = .Find(what:="sector", LookIn:=xlValues).Row
        End With

        'return the row number for the sector header
        LastCol = ws.Cells(20, ws.Columns.Count).End(xlToLeft).Column
        LastRow = ws.Range("a15").End(xlDown).Row

        'I am going to add the code below to finish out the task that I want to complete

    End If
Next    
End Sub
sub gotoEx()

for i = 1 to 10
    if i = 5 then
        goto jumpToHere
    end if
next i

jumpToHere: '<~~ the code will come here when i = 5
    'do some more code

end sub
for each ws in wb.Worksheets
    if ws.Range("a9").Value = "x" then
        goto jumpToHere
    end if

    'the rest of your code goes here

jumpToHere:
next