Excel工作簿打开时自动运行VBA代码

问题描述:

打开Excel工作簿时,我要运行VBA代码.

I have VBA code in I would like to run when the Excel workbook is opened.

我尝试在应该在其中运行代码的工作表中创建一个公共过程:

I tried creating a public procedure in the sheet the code is supposed to run in:

Public Sub Workbook_Open 
    ' Some code here
End Sub

在工作簿打开时它不会运行.

It does not run when the workbook opens.

应该在其中一个单元格中创建一个组合框,然后用数据库中的信息填充它.

It is supposed to create a combobox in one of the cells and then fill it with information from the database.

确保代码位于VBA编辑器的 ThisWorkbook 范围内,并且不是或工作表:

Make sure that the code is in the ThisWorkbook scope of the VBA editor and not in a module or worksheet:

Option Explicit

Private Sub Workbook_Open()
    MsgBox "Autorun works!"
    'your code here
End Sub

并确保您的有关详细信息,请参阅Microsoft文档:

For details also see Microsoft's documentation: Automatically run a macro when opening a workbook.