Excel VBA代码来记住之前选择的文件
我希望Bos和Dos选择之前在Bos上提示的相同文件 当我使用ADailyReport宏时,它会再次提示文件Excel VBA代码来记住之前选择的文件,excel,vba,Excel,Vba,我希望Bos和Dos选择之前在Bos上提示的相同文件 当我使用ADailyReport宏时,它会再次提示文件 Sub Bos() Dim filename__path As Variant filename__path = Application.GetOpenFilename( _ FileFilter:="Excel Files (*.XLSX), *.XLS", _ Title:="Sele
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
您可以为Dos创建变量引用,只需在Bos中调用它,反之亦然。
比如:
Sub Bos()
'~~> rest of your code here for Bos
'~~> then call Dos
Dos filename_path
End Sub
另一方面,Dos的设置应如下所示:
Sub Dos(mypath As Variant)
If mypath = False Then Exit Sub
Workbooks.Open mypath
'~~> rest of the code here
End Sub
这样,在执行Bos时,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.但值得注意的是,只要文件未关闭,或直到没有遇到使宏中断的错误,全局/公共变量就会一直存在。因此,要加载不同的文件(加载第一个文件后),需要先保存并关闭该文件。或者,如果用户希望加载内存中存储的文件以外的其他文件,则可以添加额外的查询