Excel VBA:Workbook_Open

dud*_*dud 1 excel vba worksheet

我正在使用Workbook_Open在打开应用程序时调用userform,这很正常.但是我希望它只在第一次打开时运行.我试过这个,如果我从编辑器运行sub而不是打开文件时它可以工作.

Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
     QuickStartForum.Show
End If
End Sub
Run Code Online (Sandbox Code Playgroud)

注意:A1包含将在用户表单运行后填充的值

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

这有什么解决方法还是我需要采取不同的方法?

Chr*_*ris 5

我认为这是因为你有这个代码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
Run Code Online (Sandbox Code Playgroud)