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

后端 未结 6 562
自闭症患者
自闭症患者 2020-12-11 01:07

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 op

相关标签:
6条回答
  • 2020-12-11 01:07

    Similar to Nazim's event enable via code solution I found out the following fix:

    the fix

    closing all excel workbooks and VBA windows and reopening the one with Workbook_Open() solved it.
    (likely due to the event enabling similar to Nazim's solution linked above).

    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.

    0 讨论(0)
  • 2020-12-11 01:11

    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. VERY STRANGE - definitely a Excel bug... just glad I finally solved it... Hope it helps someone...

    0 讨论(0)
  • 2020-12-11 01:23

    The solution I found was running the below code and then the "Open" event worked.

    Sub EventRestore()
    
        Application.EnableEvents = True
    
    End Sub
    
    0 讨论(0)
  • 2020-12-11 01:27

    Make sure your Private Sub Workbook_Open() subroutine is pasted inside of the This Workbook object and not in a Module, Form, or Sheet object.

    0 讨论(0)
  • 2020-12-11 01:32

    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:

    1. Right clicked in the relevant folder, clicked New, clicked Microsoft Excel Worksheet.
    2. Named the file "Workbook_1".
    3. Opened Workbook_1.
    4. File, Save As, changed file type to Excel Macro-Enabled Workbook (*.xlsm), Save.
    5. Wrote VBA code including a Workbook_Open() nested in ThisWorkbook.

    For whatever reason, I wondered if my problem had anything to do with the fact that the file "started as" a standard .xlsx. So I simply:

    1. Opened a new blank Workbook within Excel (Book1).
    2. File, Save As, input Workbook_2, changed filed type to Excel Macro-Enabled Workbook (*.xlsm), Save.
    3. Copied VBA code and structure from the original Workbook_1.

    Unlike the original Workbook_1, the new Workbook_2 successfully ran the Workbook_Open() sub on open. Maybe a potential cause of this problem is related to the file's type history (i.e. 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.

    0 讨论(0)
  • 2020-12-11 01:34

    Interesting. In 2009 a conflict with conditional formatting of the sheet to open is described, as in vbforum post.

    It seems that this bug still exists in excel and prevents the workbook_open event from being fired. 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.

    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
    
    0 讨论(0)
提交回复
热议问题