从另一个工作簿错误中调用宏

时间:2018-08-29 16:50:10

标签: excel vba excel-vba

我正在尝试从另一个工作簿中调用宏,并且不断出现以下错误。

无法运行宏

'IMECM_To... 

该宏可能在此工作簿中不可用,或者所有宏都可能被禁用。

我确保宏名称拼写正确,并且确保宏拼写正确。
我在网上看了一下,还没办法解决。
谢谢

这是代码

Sub move()

Dim wb As Workbook
Dim MacroFolder As String
Dim MacroWb As String

MacroFolder = "C:\Users\zhill\Desktop\macro learn\"
MacroWb = "IMECM_To_LDW_CSV_Format-20151023-for-2015Q3-for-udf-version-13.0.015.xlsm"

Set wb = Workbooks.Open(MacroFolder & MacroWb)

wb.Sheets("ALFA to Corp CSV").Cells(13, 2) = ThisWorkbook.Sheets("sheet1").Range("IntexFolderList").Cells(1, 1)
wb.Sheets("ALFA to Corp CSV").Cells(14, 2) = ThisWorkbook.Sheets("sheet1").Range("OutputFolderList").Cells(1, 1)
wb.Sheets("ALFA to Corp CSV").Cells(9, 9) = ThisWorkbook.Sheets("sheet1").Range("RunNbr").Cells(1, 1)

wb.Sheets("ALFA to Corp CSV").Application.Run (MacroWb & "!ALFAtoCorpCsvFormat")

End Sub

2 个答案:

答案 0 :(得分:0)

如果工作簿名称包含空格或某些其他特殊字符,则必须用单引号将该名称括起来,例如:

Application.Run "'Book 1.xls'!MyMacroName"

来源:Ron deBruin:How do I use Application.Run in Excel?


要确保未在Excel中禁用宏,可以执行以下操作:
File> Options> Trust Center> Trust Center Settings> Macro Settings> Enable All Macros

答案 1 :(得分:0)

代码看起来不错,可能您需要进行一些调试才能排除错误的文件名 按CTRL G,这将打开您的立即窗口

如果“ Exists?”行存在,请在以下debug.print命令中输入

。后面跟着一个空格,表示您的文件名或目录有问题。

按F8键逐行运行代码,而不是按F5

Sub move()

Dim wb As Workbook
Dim MacroFolder As String
Dim MacroWb As String

MacroFolder = "C:\Users\zhill\Desktop\macro learn\"
MacroWb = "IMECM_To_LDW_CSV_Format-20151023-for-2015Q3-for-udf-version-13.0.015.xlsm"

Debug.Print MacroFolder & MacroWb
Debug.Print "Exists?", Dir(MacroFolder & MacroWb)

Set wb = Workbooks.Open(MacroFolder & MacroWb)

wb.Sheets("ALFA to Corp CSV").Cells(13, 2) = ThisWorkbook.Sheets("sheet1").Range("IntexFolderList").Cells(1, 1)
wb.Sheets("ALFA to Corp CSV").Cells(14, 2) = ThisWorkbook.Sheets("sheet1").Range("OutputFolderList").Cells(1, 1)
wb.Sheets("ALFA to Corp CSV").Cells(9, 9) = ThisWorkbook.Sheets("sheet1").Range("RunNbr").Cells(1, 1)

wb.Sheets("ALFA to Corp CSV").Application.Run (MacroWb & "!ALFAtoCorpCsvFormat")

End Sub

如果您仍然似乎无法解决问题 运行此代码,以查看excel在目录中提取了哪些文件。

Sub PrintDirectoryFiles()

Dim myfile As String
Dim MacroFolder As String

MacroFolder = "C:\Users\zhill\Desktop\macro learn\"
myfile = Dir(MacroFolder & "*.xlsm")  'or *.*

Do While myfile <> ""
    Debug.Print myfile
    myfile = Dir
Loop



End Sub