在VBA中运行导入的宏时出现问题

在VBA中运行导入的宏时出现问题,vba,excel,automation,Vba,Excel,Automation,我想从指定文件夹中所有工作簿的指定位置导入宏文件,并想在每个工作簿上运行宏,我可以通过下面提到的代码成功地将其导入所有工作簿,但无法运行这些宏 Sub RecursiveFolders() Dim FileSys As Object Dim objFolder As Object Dim objSubFolder As Object Dim objFile1 As Scripting.File Dim wkbOpen As Workbook Dim

我想从指定文件夹中所有工作簿的指定位置导入宏文件,并想在每个工作簿上运行宏,我可以通过下面提到的代码成功地将其导入所有工作簿,但无法运行这些宏

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
这段代码会弹出无法运行宏的窗口

它可能不可用或所有宏都已禁用


但是没有密码和保护,因此我无法运行宏,请提供帮助。

调用宏时需要包含工作簿名称(未测试):


因为您没有使用
Option Explicit
您没有看到您的问题,因此我建议以后在编辑器中始终使用
Option Explicit
来标记此类内容

这里的问题是

Application.Run HeaderChange_User_Financial_Input
VBA假设
HeaderChange\u User\u Financial\u Input
是包含宏引用的变量。但是这个变量是空的,因为它从来没有被设置过,所以它找不到那个宏

我假设您的意思是将
HeaderChange\u User\u Financial\u Input
作为宏名称而不是变量,因此请使用字符串

Application.Run "HeaderChange_User_Financial_Input"

对于所有
应用程序。在代码中运行

宏安全设置是否设置得太高?在
Excel 2010
-
文件中
~
选项
~
信任中心
~
信任中心设置
~
宏设置
~确保未设置在不通知的情况下禁用所有宏。我通常建议禁用带有通知集的所有宏。
Application.Run "HeaderChange_User_Financial_Input"