如何在 Excel VBA 中显示警报消息?

How to show alert message in Excel VBA?

我有一个 excel (另存为 Excel 启用宏的工作簿) 文件,如下所示:

现在,我创建了这个功能,可以在用户打开文件时通知他。

Private Sub Workbook_Open()
    Dim i As Long, lastRow As Long

    lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To lastRow
        If DateDiff("d", Cells(i, 4), Date) <= 90 Then
            MsgBox (Cells(i, 2) + " is expiring!")
        End If
    Next

End Sub

程序设置为 Open,对象设置为 Workbook,我现在的问题是它不显示警报消息,除非我执行此操作:打开 Visual Basic 开发人员选项卡 -> 重新打开文件 -> 并收到此警告消息:Warning: It is not possible to determine that this content came from a trustworthy source. You should leave this content disabled unless the content provides critical functionality and you trust its source 然后单击 Enable Macros.

我正在使用 excel-2007

非常感谢任何帮助。

这是一项安全功能。在任何代码可以 运行 之前,用户必须启用宏。您无法在代码中做任何事情来防止这种情况发生。

如果文件始终在同一个文件夹中,您可以add that folder as a trusted location这样就不会出现警告:

  1. Select File > Options.
  2. Click Trust Center, then Trust Center Settings...
  3. Click Trusted Locations.
  4. If [the folder containing the file] is not listed as a Trusted Location, click 'Add new location...'. Enter the path [...] or click Browse... to locate and select it.
  5. Click OK.