簡體   English   中英

Excel VBA:工作簿_打開

[英]Excel VBA: Workbook_Open

我正在使用 Workbook_Open 在打開應用程序時調用用戶表單,這工作正常。 但是我希望它只在第一次打開時運行。 我試過這個,如果我從編輯器運行 sub ,它會工作,但當我打開文件時不會。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM