2017-04-06 58 views
1

我想用下面的問題協助懇請:application.run(宏,ARG1,Arg2)將

我試圖運行運行多個宏宏,但它是基於2串如下:

Sub ExportDatatoCountriesSheets() 

    Dim shtnme As String 
    Dim country As String 

    ' United States 
    shtnme = "US" 
    country = "United States" 
    ' ClearLatestData 
    Application.Run "ClearLatestData" 
    ' FilterExportDataByCountry 
    Application.Run "FilterExportDataByCountry" 

    ' Japan 
    shtnme = "JP" 
    country = "Japan" 
    ' ClearLatestData 
    Application.Run "ClearLatestData" 
    ' FilterExportDataByCountry 
    Application.Run "FilterExportDataByCountry" 

End Sub 

'============================================================== 

Sub ClearLatestData() 

    Dim shtnme As String 
    Dim country As String 
    Sheets(CStr(shtnme)).Activate 
    Columns("A:Z").Select 
    Selection.Clear 

End Sub 

'============================================================== 

Sub FilterExportDataByCountry 
Sheets("WEEKLY DATA").Select 
    ActiveSheet.Range("$A$1:$G$240").AutoFilter Field:=3, Criteria1:=CStr(country) 
    Columns("A:G").Select 
    Selection.Copy 
    Sheets(CStr(shtnme)).Activate 
    Range("A1").Select 
    ActiveSheet.Paste 

但是,我不斷收到錯誤。我基本上想要設置ExportDatatoCountriesSheets宏來重複,而我輸入的代碼,並運行其他應用程序。運行

我搜索和最接近的解決方案是運行宏的參數,但當我定義參數的宏無法正常運行併發生錯誤。

Application.Run (ClearLatestData, shtnme = "US", country = "United States") 

如果可能請告知。

回答

1

爲了所謂的 「ClearLatestData」 MACRO(Sub),你需要在2個參數添加到receiveing Sub。因此,語法應爲:

Sub ClearLatestData(shtnme As String, country As String) 

Sub ClearLatestData裏面,你會刪除2行宣佈String變量:

Dim shtnme As String 
Dim country As String 

要調用Sub ClearLatestDatashtnmecountry,用途:

Application.Run "ClearLatestData", shtnme, country 

或者,「更乾淨」的較短版本:

ClearLatestData shtnme, country 

代碼

Sub ExportDatatoCountriesSheets() 

Dim shtnme As String 
Dim country As String 

' United States 
shtnme = "US" 
country = "United States" 
' ClearLatestData 
ClearLatestData shtnme, country ' <-- call the MACRO  
' FilterExportDataByCountry 
Application.Run "FilterExportDataByCountry" 

' Japan 
shtnme = "JP" 
country = "Japan" 

' ClearLatestData 
ClearLatestData shtnme, country ' <-- call the MACRO  
' FilterExportDataByCountry 
Application.Run "FilterExportDataByCountry" 

End Sub 

' ================================================================ 

Sub ClearLatestData(shtnme As String, country As String) 

    Sheets(CStr(shtnme)).Activate 
    Columns("A:Z").Select 
    Selection.Clear 

End Sub 

注意:我們建議從ActivateSelectSelection遠離,並使用合格的WorksheetsRange S和其他對象來代替。

例如,而不是3線以下:

Sheets(CStr(shtnme)).Activate 
Columns("A:Z").Select 
Selection.Clear 

您可以使用一個單一的線(它會更快,因爲你不使用Activateselect):

Sheets(shtnme).Columns("A:Z").Clear 
+0

工作像一個魅力先生。感謝你的協助!非常感激。 –

+0

@ShahA歡迎您通過點擊答案旁邊的greay複選標記(標記爲綠色),標記爲「答案」 –

相關問題