2013-11-25 23 views
-1

我希望Bos和Dos選擇Bos之前提示的文件。VBA代碼記住之前選擇的文件

當我使用ADailyReport宏時,它會再次提示文件。

Sub Bos() 
    Dim filename__path As Variant 
    filename__path = Application.GetOpenFilename(_ 
        FileFilter:="Excel Files (*.XLSX), *.XLS", _ 
        Title:="Select File To Be Opened") 
    If filename__path = False Then Exit Sub 
    Workbooks.Open Filename:=filename__path 
    Sheets("HSE").Select 
    Range("L23:M23").Select 
    Selection.Copy 
    Windows("Follow-up .xlsm").Activate 
    Sheets("BE803").Select 
    ActiveWindow.SmallScroll Down:=270 
    Range("B431").Select 
    ActiveSheet.Paste 
End Sub 

Sub Dos() 
    Dim filename__path As Variant 
    filename__path = Application.GetOpenFilename(_ 
        FileFilter:="Excel Files (*.XLSX), *.XLS", _ 
        Title:="Select File To Be Opened") 
    If filename__path = False Then Exit Sub 
    Workbooks.Open Filename:=filename__path 
    Sheets("HSE").Select 
    Range("L24:M24").Select 
    Selection.Copy 
    Windows("Follow-up .xlsm").Activate 
    Sheets("BE803").Select 
    ActiveWindow.SmallScroll Down:=270 
    Range("D431").Select 
    ActiveSheet.Paste 
End Sub 

Sub ADailyReport() 
Application.Run "'C:\Users\Follow-up .xlsm'!Bos" 
Application.Run "'C:\Users\Follow-up .xlsm'!Dos" 
End Sub 

回答

0

您可以爲Dos創建一個變量引用,並在Bos中調用它,反之亦然。
喜歡的東西:

Sub Bos() 
    '~~> rest of your code here for Bos 
    '~~> then call Dos 
    Dos filename_path 
End Sub 

該做什麼,另一方面要建立這樣的:

Sub Dos(mypath As Variant) 
    If mypath = False Then Exit Sub 
    Workbooks.Open mypath 
    '~~> rest of the code here 
End Sub 

這種方式,執行博斯當DOS下自動運行。

另一種方法是你可以使用一個全局/公共變量
所以Bos和Dos仍然獨立運行,但如果其中一個已經執行,它不會提示輸入文件。

Option Explicit 
Public filename_path As Variant 

Sub Bos() 
    If IsEmpty(filename_path) Then 
     filename_path = Application.GetOpenFilename(_ 
       FileFilter:="Excel Files (*.XLSX), *.XLS", _ 
       Title:="Select File To Be Opened") 
    End If 

    If filename_path = False Then Exit Sub 
    '~~> Rest of your code here 
End Sub 

以與上述相同的方式設置Dos。 HTH。
值得注意的是,只要文件沒有關閉或者沒有遇到任何錯誤使宏中斷,變量就會持續存在。因此,爲了加載不同的文件(加載第一個文件後),您需要先保存並關閉文件。或者,如果用戶想要將不同的文件加載到存儲器中,則可以添加其他查詢。