Excel VBA:Workbook_Open

我正在使用Workbook_Open打开应用程序时调用一个用户窗体,这工作正常。 不过,我希望它只在第一次打开时运行。 我试过这个,它工作,如果我从编辑器运行子,但不是当我打开文件。

Sub Workbook_Open() If Worksheets("DataSheet").Range("A1").Value = "" Then QuickStartForum.Show End If End Sub 

注意:A1包含一个在用户窗体运行之后将被填充的值

看来问题是在数据加载到工作表之前,它会打开用户表单。

这是否有解决方法,或者我需要采取不同的方法?

我认为这是因为你在一个Module有这个代码。 您需要将代码放在“ ThisWorkBook ”中。

我试过下面的代码,并没有问题,当它在' ThisWorkBook '它未能运行'模块Module1 '

 Private Sub Workbook_Open() If Worksheets("DataSheet").Range("A1").Value = "" Then QuickStartForum.Show Worksheets("DataSheet").Range("A1").Value = "filled" ' <-- this fills the cell with data for testing, so that when you reopen the file it should not re-open the userform Else MsgBox ("not shown because the A1 cell has data") End If End Sub