Results 1 to 2 of 2

Workbook Event Producing an Error

Threaded View

  1. #1
    Forum Contributor BeachRock's Avatar
    Join Date
    11-01-2011
    Location
    Oregon, United States
    MS-Off Ver
    Excel 2016
    Posts
    403

    Workbook Event Producing an Error

    Hello,

    I have a workbook that I want to turn off (or hide) the Ribbon, Formula Bar, Status Bar and the Worksheet Tabs and then go to a specific sheet when opened. I found some code that seems to do all of that but comes up with an error message during opening and closing the file, and when opening, it also ends up not going to the specific worksheet that I want it to. The original code I found is located on this web page: http://www.mrexcel.com/forum/excel-q...-workbook.html

    I placed all of it, with the addition of calling a macro to cause the worksheet I want to be viewed, into ThisWorkbook. The reason for the Workbook_Activate and Deactivate is so these events only affect this specific workbook and not others that are opened. The code looks like the following:

    Option Explicit
    
    Private Sub Workbook_Open()
    
    Set myRange = ActiveSheet
       Application.ScreenUpdating = False
       Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
       Application.DisplayFormulaBar = False
       Application.DisplayStatusBar = Not Application.DisplayStatusBar
       ActiveWindow.DisplayWorkbookTabs = False
    
       Dim wbBook As Workbook
       Dim wsSheet As Worksheet
    
       Set wbBook = ThisWorkbook
    
       For Each wsSheet In wbBook.Worksheets
          If Not wsSheet.Name = "Blank" Then wsSheet.Activate
             With ActiveWindow
                .DisplayHeadings = False
                .DisplayGridlines = False
                ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingRows:=True
                'ActiveSheet.EnableSelection = xlUnlockedCells
             End With
       Next wsSheet
    
    myRange.Select
    
        Call GoTo_Start
        
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Set myRange = ActiveSheet
       Application.ScreenUpdating = False
       Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
       Application.DisplayFormulaBar = True
       Application.DisplayStatusBar = True
       ActiveWindow.DisplayWorkbookTabs = True
    
       Dim wbBook As Workbook
       Dim wsSheet As Worksheet
    
       Set wbBook = ThisWorkbook
    
       For Each wsSheet In wbBook.Worksheets
          If Not wsSheet.Name = "Blank" Then wsSheet.Activate
             With ActiveWindow
               .DisplayHeadings = True
               .DisplayGridlines = True
               ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingRows:=True
              'ActiveSheet.EnableSelection = xlUnlockedCells
             End With
       Next wsSheet
    
    myRange.Select
    
    End Sub
    
    Private Sub Workbook_Activate()
    
        Application.ScreenUpdating = False
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
        Application.DisplayFormulaBar = False
        Application.DisplayStatusBar = Not Application.DisplayStatusBar
        ActiveWindow.DisplayWorkbookTabs = False
        Application.ScreenUpdating = True
    
    End Sub
    
    Private Sub Workbook_Deactivate()
    
        Application.ScreenUpdating = False
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
        Application.DisplayFormulaBar = True
        Application.DisplayStatusBar = True
        Application.ScreenUpdating = True
    
     End Sub
    When executed upon opening, it hides everything as it is supposed to but doesn't run the macro "GoTo_Start" (bolded above) to land on the worksheet I want it to. It then pops up the following error message: "Compile error in hidden module: ThisWorkbook. This error commonly occurs when code is incompatible with the version, platform, or architecture of this application." When I click the Ok button I end up on whichever sheet was visible when last saved instead of the correct start sheet. I can then do whatever I want to the workbook, adding records, etc., but when I go to close the file, I receive another error message: "Compile error: Variable not defined" and it then highlights the areas in yellow(ish) and blue font above. After stopping the code I can save the file and close it.

    Thank you very much for any help or suggestions you can offer!
    Last edited by BeachRock; 02-12-2014 at 04:35 PM.
    -------------
    Tony

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Code producing error, only on third run of Userform...?
    By RayJay01 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-12-2019, 10:01 PM
  2. [SOLVED] Nest LEFT in VLOOKUP producing error
    By msawyer in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-07-2012, 07:26 PM
  3. Array loop producing error "Run time error '1004'"
    By jeskit in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-06-2011, 08:56 AM
  4. VBA code suddleny producing an error
    By drgogo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2010, 07:18 AM
  5. recorded macro for pagesetup producing error
    By suprme32excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-27-2009, 05:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1