我尝试在google上搜索这个但没有用,我想通过Application.GetOpenFilename函数打开一个文件并复制&从源文件粘贴到目标文件,但每次打开它都会弹出一个弹出窗口来启用/禁用宏。
我试过了
Application.DisplayAlerts = False
Application.EnableEvents = False
在代码中的任何地方,它都有助于抑制所有其他事件,但不会抑制启用宏事件。
以下是我的代码:
Sub Browse_WeeklyFile()
Dim strFileToOpen As String
Dim Wbk_WeeklyTemplate As Workbook
Dim Tgt_ShtNm As String
Dim Src_ShtNm As String
'Choosing an Excel File using File dialog Box and capturing the file path in the variable
Application.DisplayAlerts = False
Application.EnableEvents = False
strFileToOpen = Application.GetOpenFilename(Title:="Please select the latest Weekly template to open", FileFilter:="Excel Files *.xlsm (*.xlsm),", MultiSelect:=False)
'Checking if file is selected
If strFileToOpen = "False" Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Set Wbk_WeeklyTemplate = Workbooks.Open(strFileToOpen)
End If
''Refer sheet by Sheet CodeName
Src_ShtNm = SheetName(Wbk_WeeklyTemplate, "Sht_FactoryDetails")
Tgt_ShtNm = SheetName(ThisWorkbook, "Sht_Ref")
Wbk_WeeklyTemplate.Sheets(Src_ShtNm).Range("N15:X18").Copy
ThisWorkbook.Sheets(Tgt_ShtNm).Range("A1").PasteSpecial xlPasteValues
Wbk_WeeklyTemplate.Close
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
有没有办法让新文件只读或禁止启用宏事件。
非常感谢您的帮助。
PS:SheetName是一个用户定义的函数,用于按工作表代码名称引用工作表名称。