【问题标题】:Error 1004 after restart: macro may not be available in this workbook or all macros may be disabled重新启动后出现错误 1004:此工作簿中的宏可能不可用或所有宏可能已禁用
【发布时间】: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


【解决方案1】:

我不知道发生了什么,但是在关闭笔记本电脑(正确关闭)后,一切又开始工作了。

重启不起作用(我尝试了 3 次),但关闭再打开确实让它再次起作用。

一个相关的变化是在继续之前开始显示以下警告:

“隐私警告:此文档包含宏、ActiveX 控件、XML 扩展包信息或 Web 组件。这些可能包括文档检查器无法删除的个人信息”

如果有人知道可能发生的事情,那就太棒了。

干杯

【讨论】:

    猜你喜欢
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 2017-03-09
    • 2013-08-25
    相关资源
    最近更新 更多