从 Excel 中打开文件/工作簿时宏不运行

从 Excel 中打开文件/工作簿时宏不运行

我对宏和 VBA 还不熟悉,但我认为我的目标很简单。我的宏应该搜索打开的 Excel 文档(自动),并创建一个带有警告的消息框,如果WEEKNUM()函数仅使用一个参数(日期)。
因此,我在 excel 2016/Visual Basic 编辑器中创建了宏,并将其保存为 .xlam(Excel 插件)。
然后我将文件复制到那里,C:\Users\User\AppData\Roaming\Microsoft\AddIns因为我猜插件就在那里。我已确保此位置在 Excel 信任中心设置中是受信任的位置 + 我已在设置中启用了所有宏。

所以我的宏看起来像这样:
Excel 对象 - ThisWorkBook:

Private Sub Workbook_Open()
  Application.OnTime Now + TimeValue("00:00:02"), "Start"
  MsgBox ("Hello World!")
End Sub

我正在使用 Workbook_Open() 事件来启动我的宏。
然后我的名为 kwTester 的模块测试文档:

Private Sub Start()
'Sheet is the current sheet
Dim Sheet As Worksheet
'my Regex
Dim regEx As New RegExp
'the search pattern
Dim strPattern As String: strPattern = "^\=WEEKNUM\(\w\d+\)$"
'is used to tell my if there is just one parameter
Dim verwendetKalenderwoche As Boolean
verwendetKalenderwoche = False
'CellAddress and CellFormula are just for debugging purposes
Dim CellAddress As Variant
Dim CellFormula As Variant
'Setting up the RegEx
If strPattern <> "" Then
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = strPattern
    End With
End If
'Code should be repeated for every opened Workbook and Sheet
'Dim book As Workbook
For Each book In Workbooks
For Each Sheet In book.Worksheets
    With Sheet.UsedRange
        For Each cell In Sheet.UsedRange
            On Error Resume Next
            CellFormula = cell.Formula
            On Error Resume Next
            If cell.Formula <> "" And regEx.Test(cell.Formula) Then
                verwendetKalenderwoche = True
                CellAddress = CellAddress & vbNewLine & Cells(cell.Row, cell.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)
            End If
        Next
    End With
Next
Next
'if there is a WEEKNUM function with just one parameter then display msgBox and UserForm
If verwendetKalenderwoche = True Then
    MsgBox "Sie verwenden die Funktion KALENDERWOCHE mit nur einem Parameter. Ab Office 2007 wird hier die amerikanische Rechenweise zur Bestimmung der Kalenderwoche verwendet." & vbNewLine & "Diese ist nicht mit der geltenden ISO-Regelung in Deutschland kompatibel." & vbNewLine & "Biite verwenden Sie wenn möglich den zusätzlichen Parameter <21> um diesen Fehler zu beheben." & vbNewLine & "Beispiel:" & vbNewLine & "KALENDERWOCHE(A2;21)", vbExclamation, "KALENDERWOCHE-Warnung"
    'UserForm anzeigen um die betroffenen Zellen darzustellen
    UserForm1.TextBox1.MultiLine = True
    UserForm1.TextBox1.Text = CellAddress
    UserForm1.Caption = "Betroffene Zellen"
    UserForm1.TextBox1.ScrollBars = fmScrollBarsVertical
    UserForm1.Show vbModeless
End If
End Sub

用户窗体不是那么重要所以我不会将其包括在内,它只显示包含 WEEKNUM 函数的单元格。

如果我打开使用 WEEKNUM 函数的 excel 文档,一切都会正常工作 - 显示我的消息框,我收到我的用户表单,这会告诉我哪些单元格正在使用 WEEKNUM 函数。这正是我想要的,所以我猜我的代码在某种程度上是有效的。:)

我使用以下网站来编写代码:
打开 Excel 时自动运行 Excel 宏
运行宏

我的问题描述

Excel 已关闭 - 我双击打开一个文件 - 我的宏已成功执行并显示我的结果。但如果最小化此 Excel 实例并打开另一个文件,我的宏就不会再次执行。如果我通过文件打开对话框从 Excel 中打开文档,情况也是如此。
哪里出了问题?
Workbook_Open 事件的工作方式应该是这样的吗?还是我遗漏了什么?
我的加载项是否位于正确的位置?我是否需要检查其他设置?

我希望我的问题不会被视为偏离主题。我不确定在哪里提问 - Stackoverflow 还是 Superuser。如果我应该删除此问题,请通知我 :)

欢迎任何意见。

答案1

您正在尝试处理Workbook事件,这些事件是它们所属对象(在本例中为插件)的本地事件。您需要的是一个Application事件。奇普·皮尔森有一个页面解释这个概念。

总而言之,将此代码放入ThisWorkbook您的插件对象内。

Option Explicit
Private WithEvents App As Application

Private Sub Workbook_Open()
     Set App = Application
 End Sub

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    MsgBox "Hello world"
End Sub

答案2

我在 Excel 上修复了这个问题。如果工作簿中的任何工作表保存在分页预览中,则宏在打开时将不起作用!一旦我将其切换回正常视图,一切又开始正常工作。祝你好运!

相关内容