2016-11-18 219 views
0

花了一些時間研究,我無法弄清楚什麼是錯的。從MS Access VBA運行Excel VBA

這兩個文件中的安全設置都是正確的。被調用的子文件在公共模塊中是公開的&。

該錯誤提示「無法運行宏等等等等等等......」

代碼:

Private Sub this() 
    Dim xl As Object 
    Set xl = CreateObject("Excel.Application") 
    xl.Visible = True 
    xl.Workbooks.Open "pathToFile" & "\" & "Dig IT.xlsm", True, False 
    xl.Run "ThisWorkbook.Module3" 
    Set xl = Nothing 
End Sub 
+0

上帝啊IM啞。更改爲xl.Run「名稱」,它的工作完美。作爲回答發佈並接收反饋 –

回答

1

相反的"ThisWorkbook.Module3",您需要提供的工作簿名稱,模塊名稱和程序名稱(假設「Module3」是代碼模塊的名稱,而不是您嘗試運行的子/函數)。

我認爲,應該是這樣的(未經測試)格式:

Private Sub this() 
    Dim xl As Object, wb as Object 
    Const MODULE_NAME As String = "Module3" '### MODIFY AS NEEDED! 
    Const PROC_NAME As String = "macro_name" '### MODIFY AS NEEDED! 
    Dim xlMacro As String 
    Set xl = CreateObject("Excel.Application") 
    xl.Visible = True 
    Set wb = xl.Workbooks.Open("pathToFile" & "\" & "Dig IT.xlsm", True, False) 

    ' builds the string to send to Excel.Run method: 
    xlMacro = wb.Name & "!" & MODULE_NAME & "." & PROC_NAME 
    ' call the macro in Excel 
    xl.Run xlMacro 
    Set xl = Nothing 
End Sub