简体   繁体   English

当我打开工作簿时,Workbook_Open sub 不会运行?

[英]Workbook_Open sub won't run when I open the workbook?

This program is supposed to create a button that the user can press to activate a different sub.这个程序应该创建一个按钮,用户可以按下它来激活不同的子程序。 From my searches online, it seems that the sub below should activate when opening up the workbook, but it's not?从我在网上的搜索来看,打开工作簿时似乎应该激活下面的子项,但事实并非如此?

What am I doing wrong?我究竟做错了什么?

Option Explicit
Private Sub Workbook_Open()
Dim btn As Button
Dim rng As Range
With Worksheets("Sheet1")
    Set rng = .Range("B2:C2")
        Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
    With btn
        .Caption = "To begin the program, please click this button"
        .AutoSize = True
        .OnAction = "TableCreation1"
    End With
End With
End Sub

确保您的Private Sub Workbook_Open()子例程粘贴在This Workbook对象中,而不是在 Module、Form 或 Sheet 对象中。

Interesting.有趣的。 In 2009 a conflict with conditional formatting of the sheet to open is described, as in vbforum post .在 2009 年,描述了与要打开的工作表的条件格式的冲突,如vbforum post

It seems that this bug still exists in excel and prevents the workbook_open event from being fired.似乎这个错误仍然存​​在于 excel 中,并阻止了workbook_open事件被触发。 I have a workbook (old XLS-binary format) that simply does not fire the event in Excel 2003 and 2007 but does in 2013. I deleted all conditional formatting from the first worksheet but could still not get the workbook_open procedure to run in elder Excel-Versions.我有一个工作簿(旧的 XLS 二进制格式),它根本不会在 Excel 2003 和 2007 中触发事件,但会在 2013 年触发。我从第一个工作表中删除了所有条件格式,但仍然无法让workbook_open过程在旧 Excel 中运行- 版本。

A Workaround, I use in distributed workbooks is to use a local variable and a second event in the workbook as follows:我在分布式工作簿中使用的解决方法是在工作簿中使用局部变量和第二个事件,如下所示:

''
' private variable
Private wbOpenEventRun as Boolean

''
' procedure to be called by excel when workbook opens
Private Sub Workbook_Open()
    wbOpenEventRun = true
    ' perform tasks
End Sub

''
' the selection change event fires usually.
' performance is not reduced
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Not wbOpenEventRun Then Workbook_Open
    ' perform tasks in reaction of selection change events, if required
End Sub

The solution I found was running the below code and then the "Open" event worked.我找到的解决方案是运行以​​下代码,然后“打开”事件起作用。

Sub EventRestore()

    Application.EnableEvents = True

End Sub

I know this post has been dormant for a while, but I just struggled for hours to solve this problem.我知道这篇文章已经沉寂了一段时间,但我只是努力了几个小时来解决这个问题。 It's the oddest thing, but I finally noticed that one of my worksheets was in "Page View"... and as soon as I put it into "Normal" my Workbook_Open() function started working as normal again.这是最奇怪的事情,但我终于注意到我的一个工作表在“页面视图”中......一旦我将它放入“正常”,我的 Workbook_Open() 函数又开始正常工作了。 VERY STRANGE - definitely a Excel bug... just glad I finally solved it... Hope it helps someone...非常奇怪 - 绝对是一个 Excel 错误......很高兴我终于解决了它......希望它可以帮助某人......

My solution was kind of obscure, and I don't even remember now why it occurred to me as a potential solution.我的解决方案有点晦涩,现在我什至不记得为什么我认为它是一个潜在的解决方案。 The file I created in which the Workbook_Open() macro would not run, I originally created through the following steps:我创建的 Workbook_Open() 宏无法运行的文件,我最初是通过以下步骤创建的:

  1. Right clicked in the relevant folder, clicked New , clicked Microsoft Excel Worksheet .右键单击相关文件夹,单击New ,单击Microsoft Excel Worksheet
  2. Named the file " Workbook_1 ".将文件命名为“ Workbook_1 ”。
  3. Opened Workbook_1 .打开Workbook_1
  4. File, Save As, changed file type to Excel Macro-Enabled Workbook (*.xlsm) , Save.文件,另存为,将文件类型更改为Excel 启用宏的工作簿 (*.xlsm) ,保存。
  5. Wrote VBA code including a Workbook_Open() nested in ThisWorkbook .编写了包含嵌套在ThisWorkbook 中Workbook_Open() 的VBA 代码。

For whatever reason, I wondered if my problem had anything to do with the fact that the file "started as" a standard .xlsx .无论出于何种原因,我想知道我的问题是否与文件“开始为”标准.xlsx的事实有关。 So I simply:所以我简单地:

  1. Opened a new blank Workbook within Excel ( Book1 ).在 Excel ( Book1 ) 中打开了一个新的空白工作簿。
  2. File, Save As, input Workbook_2 , changed filed type to Excel Macro-Enabled Workbook (*.xlsm) , Save.文件,另存为,输入Workbook_2 ,将文件类型更改为Excel Macro-Enabled Workbook (*.xlsm) ,保存。
  3. Copied VBA code and structure from the original Workbook_1 .从原始Workbook_1复制 VBA 代码和结构。

Unlike the original Workbook_1 , the new Workbook_2 successfully ran the Workbook_Open() sub on open.与原始Workbook_1不同,新Workbook_2在打开时成功运行Workbook_Open()子。 Maybe a potential cause of this problem is related to the file's type history (ie if it was at some point a type that cannot run macros).也许这个问题的潜在原因与文件的类型历史有关(即,如果它在某个时候是不能运行宏的类型)。 Or maybe simply trying again with a new file is what solved this problem for me.或者也许只是用一个新文件再试一次就是为我解决这个问题的方法。 Either way, this may work for anyone for which other solutions did not.无论哪种方式,这可能适用于其他解决方案不适用的任何人。

Similar to Nazim's event enable via code solution I found out the following fix:通过代码解决方案启用 Nazim 的事件类似,我发现了以下修复:

the fix修复

closing all excel workbooks and VBA windows and reopening the one with Workbook_Open() solved it.关闭所有 excel 工作簿和 VBA 窗口并使用Workbook_Open()重新打开一个解决了它。
(likely due to the event enabling similar to Nazim's solution linked above). (可能是由于事件启用类似于上面链接的Nazim 的解决方案)。

problem experience问题经验

As I was debugging and aborted execution, I disabled the events in the running code before abort.在调试和中止执行时,我在中止之前禁用了正在运行的代码中的事件。 I did not notice this circumstance at first and even after close/reopen of my workbook it did not work...我一开始没有注意到这种情况,即使在关闭/重新打开我的工作簿后它也不起作用......

probable cause可能的原因

...The other open excel workbooks or some global instance somehow "remembered" that the events of the reopened workbook had been turned off. ...其他打开的 excel 工作簿或某个全局实例以某种方式“记住”了重新打开的工作簿的事件已被关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM