2017-07-25 129 views
-1

我想从指定位置的指定位置导入指定文件夹中的所有工作簿中的宏文件,并且希望在每个工作簿上运行宏,我可以通过所提到的代码成功地在所有工作簿中导入它下面但不能运行这些宏。在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 

此代码给我弹出无法运行宏,

它可能不可用或所有的宏被禁用

但没有密码保护所以为什么我不能运行宏,请帮忙。

+0

您是否将宏安全设置设置得太高?在'Excel 2010' - 'File'〜'Options'〜'Trust Center'〜'Trust Center Settings'〜'Macro Settings'〜确保_Disable所有没有notification_的宏没有设置。我通常建议通过notification_set禁用所有的宏。 –

回答

0

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

strFile = ActiveWorkbook 
Application.Run "'" & strFile.Name & "'!HeaderChange_User_Financial_Input" 
0

因为你没有使用Option Explicit你没有看到您的问题,因此,我建议经常使用Option Explicit来将来在编辑器中标记这类事情。

这里的问题是,在

Application.Run HeaderChange_User_Financial_Input 

VBA假定HeaderChange_User_Financial_Input是含有宏引用一个可变。但是这个变量是空的,因为它从不设置,因此它找不到那个宏。

我假设你的意思HeaderChange_User_Financial_Input是宏的名称,而不是一个变量,这样使用一个字符串,而不是

Application.Run "HeaderChange_User_Financial_Input" 

代码中的所有Application.Run