2016-08-30 61 views
0

我最新的問題是參考this post。我有一個Master macro,它在工作簿中每頁打一個macro。我收到一個Subscript out of range錯誤響應,從我在前一篇文章中的「摘要」表中的macro到下一張表中的另一個macro。我想我可以消除錯誤,並使Master macro工作,如果我可以消除工作表.Select語句並在一行中識別工作表時調用宏。任何幫助?VBA-每頁打印宏問題

它的現在,與未來的錯誤Call ReformatSummarySheets("Boston").Select後:

Sub ReformatTheWorkbook() 

    Sheets("Summary").Select 
    Call ReformatSummary 

    Sheets("Boston").Select 
    Call ReformatSheetAndAddDropdowns 

    Sheets("London").Select 
    Call ReformatSheetAndAddDropdowns 

    Sheets("Hong Kong").Select 
    Call ReformatSheetAndAddDropdowns 

End Sub 

這是我想做的事情,但沒有Sheet("name").Select不必識別下一張紙:

Sub ReformatTheWorkbook() 

Sheets("Summary").Select 
Call ReformatSummary 

Application.Run "RefreshAllStaticData" 
Application.OnTime Now + TimeValue("00:00:05"), "Part2RTW" 

End Sub 


Sub Part2RTW() 

Sheets("Boston").Select 
Call ReformatSheetAndAddDropdowns 

Sheets("London").Select 
Call ReformatSheetAndAddDropdowns 

Sheets("Hong Kong").Select 
Call ReformatSheetAndAddDropdowns 

End Sub 
+0

什麼是「重新格式化」摘要? – OpiesDad

+1

將工作表作爲參數傳遞。 – Comintern

+1

下標超出範圍錯誤通常表明工作表不存在....您確定您有一張名爲「波士頓」的工作表嗎? – OpiesDad

回答

2

以下是共產國際發表評論的一個例子...您應該通過工作表作爲參數:

Sub ReformatSummary(ws As Worksheet) 
    'instead of ActiveSheet.Range("A1").Value = "Test" use: 
     ws.Range("A1").Value = "Test" 
End Sub 
Sub ReformatSheetAndAddDropdowns(ws As Worksheet) 
    ....Whatever you are doing to the sheets 
End Sub 

Sub ReformatTheWorkbook() 
    Call ReformatSummary(Sheets("Summary")) 
    Call ReformatSheetAndAddDropdowns(Sheets("Boston")) 
    .... 
End Sub 

這將傳遞工作表作爲參數。請注意,當您在過程中使用參數時,您需要使用工作表,而不是您在「ActiveSheet」中可能執行的操作。

因此,如果你有代碼如 ActiveSheet.Range( 「A1」)。值= 「測試值」

,這將在所選擇的片材等於TestValue

集小區A1相反,您應該使用

ws.Range("A1").Value = "TetsValue" 

這將在對象ws等於所限定的片材設置的細胞A1TestValue

在這個例子中,正在被聲明Sheets("Summary")

正如評論指出生成的工作表對象,這不符合您遇到的錯誤消息,雖然。下標超出範圍錯誤通常表示該對象不存在。在這種情況下,我猜想沒有名爲「波士頓」的紙。

+0

對不起,我還是不太擅長VBA。如果我只是運行第一個宏ReformatSummary,那看起來怎麼樣?我很困惑,它會去哪裏。 – plankton