【问题标题】:Excel VBA: Workbook_OpenExcel VBA:工作簿_打开
【发布时间】:2020-02-28 02:21:09
【问题描述】:

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

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

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

问题似乎在于它在将数据加载到工作表之前打开了用户表单。

有没有办法解决这个问题,还是我需要采取不同的方法?

【问题讨论】:

    标签: excel vba worksheet


    【解决方案1】:

    我认为这是因为您在 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
    

    【讨论】:

    • sub 是否需要为private
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多