Excel VBA:Workbook_Open [英] Excel VBA: Workbook_Open

查看:496
本文介绍了Excel VBA:Workbook_Open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开应用程序时,我正在使用Workbook_Open调用用户窗体,这工作正常.但是,我希望它仅在第一次打开时运行. 我试过了,如果我从编辑器中运行子程序,但打开文件时却无法运行.

I'm using Workbook_Open to call a userform when the application is opened, this is working fine. However I would like it to only run the first time it is opened. I tried this and it work if I run the sub from the editor but not when I open the file.

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

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

Note: A1 contains a value that will be populated after the user form has run

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

It appears that the problem is that it opens the user form before the data is loaded into the worksheet.

这是否可以解决?还是需要采取其他方法?

Is this there a way around this or do I need to take a different approach ?

推荐答案

我认为这是因为您在Module中有此代码.您需要将代码放在"ThisWorkBook"中.

I think it is because you have this code in a Module. You need to put the code within 'ThisWorkBook'.

我尝试了以下代码,当它位于"ThisWorkBook"中时没有问题,它无法在"Module1"中运行

I tried this following code and had no issues when it was in the 'ThisWorkBook' it failed to run within '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

这篇关于Excel VBA:Workbook_Open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆