循环遍历所有工作表VBA

时间:2015-02-06 00:38:19

标签: 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 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

我确信问题在于我误解了每个循环实际上是如何工作的。希望有人能够更好地理解。

我真的很感激这方面的任何帮助。

我对代码进行了一些编辑,现在我确实遇到了错误:)我尝试使用ws.range等对#34;"进行修改。一段代码,我得到对象错误91.

以下是我的新内容"改进后的#34;码。

Sub sort_sectors()

Dim i As Integer
Dim rng As Range
Dim SortRng As Range
Dim intAnchorRow As Integer
Dim intMktCapAnchor As Integer
Dim intSectorAnchor 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 ActiveWorkbook.Worksheets

'Filter out the sheets that we don't want to run
If ws.Range("a9").Value <> "x" Or ws.Name = "__FDSCACHE__" Or ws.Name = "INDEX" Then

'Get the anchor points for getting sort range and the sort keys
''''''THIS IS THE PART THAT IS NOW GIVING ME THE ERROR'''''''
    With ws.Range("a1:t100")
        intAnchorRow = .Find(what:="sector", LookIn:=xlValues).Row
        intSectorAnchor = .Find(what:="sector", LookIn:=xlValues).Column
        intMktCapAnchor = .Find(what:="Market Cap", LookIn:=xlValues).Column
    End With

'Find the last row and column of the data range
    LastCol = ws.Cells(20, ws.Columns.Count).End(xlToLeft).Column
    LastRow = ws.Range("a15").End(xlDown).Row

    Set SortRng = Range(Cells(intAnchorRow + 1, 1), Cells(LastRow, LastCol))
    Range(SortRng).Sort key1:=Range(Cells(intAnchorRow + 1, intSectorAnchor), Cells(LastRow, intSectorAnchor)), _
        order1:=xlAscending, key2:=Range(Cells(intAnchorRow + 1, intMktCapAnchor), Cells(LastRow, intMktCapAnchor)), _
        order2:=xlDescending, Header:=xlNo


End If

Next

End Sub

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

1 个答案:

答案 0 :(得分:2)

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

如果是这种情况,我会更改if语句的条件,以检查单元格是否不包含x。如果是这样,则输入其余代码。如果没有,则进入next迭代。

此外,您的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

:运算符用于在goto调用后将代码返回到该行。

例如

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

当然,如果您愿意,可以在代码中使用此结构,并在jumpToHere:

之前设置next

e.g。

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