2015-02-06 251 views
1

我試圖循環執行activeworkbook中的所有工作表以執行重複性任務。循環遍歷所有工作表VBA

目前,我有下面的代碼:

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 etc ...」這段代碼進行了修改,我得到了object error 91.

下面是我的新「改進」代碼。

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

我應該澄清。我瀏覽代碼,我看不到任何事情發生。我甚至添加了ws.range(「a1」)。value =「something」來檢查。 – dutchballa 2015-02-06 01:08:06

+0

如果你有另一個問題,你應該開始一個新的問題,因爲這個問題已經被「解決」了。 – tospig 2015-02-10 21:13:50

回答

2

如果我已正確理解您的問題,則不希望在單元格A9中使用x的工作表。

如果是這種情況,我會更改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

例如

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 
+0

這對我有意義。感謝您的幫助。當我遍歷代碼時,看起來好像第一次通過循環時沒有任何工作表被更新。在循環的第二次運行中,我可以看到我的工作簿中第一張紙上運行的代碼。這是每個ws循環的怪癖,還是我在這裏失蹤的東西? – dutchballa 2015-02-06 01:18:25

+0

我沒有注意到有'每個ws'循環的怪癖;聽起來像其他事情正在發生。你可能需要在範圍(「a1:t100」)中包含'ws',給wsRange(「a1:t100」)+1 – tospig 2015-02-06 01:33:17

+0

@dutchballa讓我知道如果是這種情況,我我會更新我的答案。 – tospig 2015-02-06 01:34:35