我想從指定位置的指定位置導入指定文件夾中的所有工作簿中的宏文件,並且希望在每個工作簿上運行宏,我可以通過所提到的代碼成功地在所有工作簿中導入它下面但不能運行這些宏。在VBA中運行導入的宏的問題
Sub RecursiveFolders()
Dim FileSys As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim objFile1 As Scripting.File
Dim wkbOpen As Workbook
Dim szImportPath As String
Dim objFSO As Scripting.FileSystemObject
Dim cmpComponents As VBIDE.VBComponents
Set objFSO = New Scripting.FileSystemObject
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set objFolder = FileSys.GetFolder("C:\Users\Yashika Vaish\Desktop\testform")
Application.ScreenUpdating = False
For Each objSubFolder In objFolder.SubFolders
For Each objFile In objSubFolder.Files
Set wkbOpen = Workbooks.Open(Filename:=objFile)
szImportPath = FolderWithVBAProjectFiles & "C:\Macros"
Set cmpComponents = wkbOpen.VBProject.VBComponents
For Each objFile1 In objFSO.GetFolder(szImportPath).Files
If (objFSO.GetExtensionName(objFile1.Name) = "cls") Or _
(objFSO.GetExtensionName(objFile1.Name) = "frm") Or _
(objFSO.GetExtensionName(objFile1.Name) = "bas") Then
cmpComponents.Import objFile1.Path
End If
Next objFile1
Application.DisplayAlerts = False
MsgBox "Import is ready"
Application.Run "HeaderChange_User_Financial_Input"
Application.Run HeaderChange_User_Financial_Input
Application.Run HeaderChange_User_Operation_Input
Application.Run SelectRangeUnitMap
Application.Run reportingunitmap
Application.Run HeaderChange_Finacial_Standard
Application.Run HeaderChange_Operation_Standard
wkbOpen.Close savechanges:=True
Next
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
此代碼給我彈出無法運行宏,
它可能不可用或所有的宏被禁用
但沒有密碼保護所以爲什麼我不能運行宏,請幫忙。
您是否將宏安全設置設置得太高?在'Excel 2010' - 'File'〜'Options'〜'Trust Center'〜'Trust Center Settings'〜'Macro Settings'〜確保_Disable所有沒有notification_的宏沒有設置。我通常建議通過notification_set禁用所有的宏。 –