【发布时间】:2015-11-13 23:30:26
【问题描述】:
我正在运行一个宏,它打开另一个 excel 文件 (.xlsm) 并运行该文件中包含的宏。
我重新启动了我的电脑并尝试运行它。但是代码不再起作用并显示此错误:
运行时错误 1004:无法运行宏 '01.xlsm!ReadCheckBoxes1'。
该工作簿中的宏可能不可用,或者所有宏都可能被禁用。
到了这一行就报错了
Call Application.Run(Macro1)
启用所有宏被选中,信任中心在“信任对VBA项目对象模型的访问”中打勾
完整的代码如下,你能帮我吗?
Sub FileAnalysis()
Dim File As String
Dim Path As String
Dim Macro1 As String
Dim Macro2 As String
Dim b As Object
Dim Ligne As Integer
Dim wb As Workbook
Set b = ThisWorkbook.Worksheets(7).Buttons(Application.Caller)
With b.TopLeftCell
Ligne = .Row
End With
Application.ScreenUpdating = False
File = ThisWorkbook.Worksheets(7).Cells(Ligne, "B").Text
ThisWorkbook.Worksheets(7).Cells(4, 9) = File 'debug
Path = ActiveWorkbook.Path
ThisWorkbook.Worksheets(7).Cells(4, 10) = Path 'debug
If Dir(Path & "\" & File) = "" Then
MsgBox "File doesn't exist."
Else
Set wb = Workbooks.Open(Path & "\" & File)
ActiveWorkbook.Worksheets(6).Unprotect ("test")
Macro1 = File & "!" & "ReadCheckBoxes1"
Macro2 = File & "!" & "ReadCheckBoxes2"
ThisWorkbook.Worksheets(7).Cells(3, 10) = ActiveWorkbook.Name 'debug
ThisWorkbook.Worksheets(7).Cells(4, 11) = Macro1 'debug
ThisWorkbook.Worksheets(7).Cells(4, 12) = Macro2 'debug
Call Application.Run(Macro1) 'error displayed here
Call Application.Run(Macro2) 'error displayed here if I comment the previous line
wb.Close SaveChanges:=True
ThisWorkbook.Worksheets(7).Cells(Ligne, 4) = "Yes"
ThisWorkbook.Worksheets(7).Cells(4, 13) = "Done"
Application.DisplayFormulaBar = True
ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayHeadings = True
ActiveWindow.DisplayGridlines = True
End If
Application.ScreenUpdating = True
If Application.CommandBars("Ribbon").Height <= 100 Then
CommandBars.ExecuteMso "MinimizeRibbon"
End If
Set wb = Nothing
Set b = Nothing
End Sub
【问题讨论】:
-
VBA 项目是否在其他工作簿中受到保护?
-
不,不是,我刚刚仔细检查过
-
如果我打开另一个文件并运行它,宏运行良好
-
文件名中是否有空格或其他“奇数”字符?这通常会导致问题。
-
没有。一切都设置为没有空格,我还仔细检查并尝试在路径中添加'。但没用这真的让我很困惑 tbf
标签: vba excel runtime-error