Workbook open not working when run automatically

Uzma Shaheen

Active Member
Joined
Nov 10, 2012
Messages
484
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
Hi

I have some code on workbook open that checks to see the height and width of the active window and it then changes the zoom depending on which criteria it falls into

Now when I run it on the workbook open - it doesn’t change to the correct zoom view but when I step through it manually it does and I’m not sure why it only works when I step through it manually

I don’t have the code to hand at the mo and will post when I get access to a computer but was hoping someone could help and see if it’s a common issue


thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Is the code in the ThisWorkbook module?
 
Upvote 0
Would need to see the code to see what the problem is.
 
Upvote 0
Here is all the code - i dunno if excel is running so quick that it doesnt pick up the active window height/width on the workbook open code but again when i run it manually and step through - it works fine. Every code works fine when stepping through it

This workbook code

Code:
[/B]Private Sub Workbook_BeforeClose(Cancel As Boolean)


    Dim dWidth As Double
    Dim dHeight As Double
    
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
    Application.DisplayFormulaBar = True
    Application.DisplayStatusBar = True
    ActiveWindow.DisplayWorkbookTabs = True
    ActiveWindow.DisplayHorizontalScrollBar = True
    ActiveWindow.DisplayVerticalScrollBar = True
    
     dWidth = ActiveWindow.Width
     dHeight = ActiveWindow.Height
    
'=======LOADING SCREEN=============='
   
    'Home Monitor '792 height '1452 width
    If dHeight = 792 And dWidth = 1452 Then
        ActiveWindow.Zoom = 106
        
    'Desktops '792 height '1452 width
    ElseIf dHeight = 769.5 And dWidth = 1272 Then
        ActiveWindow.Zoom = 100
            
    Else
    'Use Standard Default View Zoom
        ActiveWindow.Zoom = 106
    End If
    
Application.ScreenUpdating = True


End Sub
    




Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


Application.ScreenUpdating = False


Dim dWidth As Double
Dim dHeight As Double


Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
Application.DisplayFormulaBar = True
Application.DisplayStatusBar = True
ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayHorizontalScrollBar = True
ActiveWindow.DisplayVerticalScrollBar = True
Worksheets("Home").Visible = True
Application.GoTo Sheets("HOME").Range("A1"), Scroll:=True
    
    Set ws = Sheets("HOME")
    Set shp = ws.Shapes("GROWBAR")
    shp.Width = 0
    
    dWidth = ActiveWindow.Width
    dHeight = ActiveWindow.Height
    
'=======LOADING SCREEN=============='
   
    'Home Monitor '792 height '1452 width
    If dHeight = 792 And dWidth = 1452 Then
        ActiveWindow.Zoom = 106
        
    'Desktops '792 height '1452 width
    ElseIf dHeight = 769.5 And dWidth = 1272 Then
        ActiveWindow.Zoom = 100
          
    Else
    'Use Standard Default View Zoom
        ActiveWindow.Zoom = 106
    End If
    
Application.ScreenUpdating = True


End Sub


Private Sub Workbook_Open()




Dim dWidth As Double
Dim dHeight As Double


Set ws = Sheets("HOME")


'Application.Goto Worksheets("HOME").Range("A1"), Scroll:=True


ActiveWindow.WindowState = xlMaximized
Application.EnableCancelKey = xlDisabled
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
Application.DisplayFormulaBar = False
Application.DisplayStatusBar = Not Application.DisplayStatusBar
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.DisplayHorizontalScrollBar = False
ActiveWindow.DisplayVerticalScrollBar = False
    
dWidth = ActiveWindow.Width
dHeight = ActiveWindow.Height
    
'=======LOADING SCREEN=============='
   
    'Home Monitor '792 height '1452 width
    If dHeight = 792 And dWidth = 1452 Then
        ActiveWindow.Zoom = 106


    'Desktops '792 height '1452 width
    ElseIf dHeight = 769.5 And dWidth = 1272 Then
        ActiveWindow.Zoom = 100
  
    Else
    'Use Standard Default View Zoom
        ActiveWindow.Zoom = 106
   
    End If
       
Application.ScreenUpdating = True


End Sub
Worksheet event Codes on Worksheet HOME

Code:
Private Sub Worksheet_Activate()


Dim dWidth As Double
Dim dHeight As Double


    ActiveWindow.WindowState = xlMaximized
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
    Application.DisplayFormulaBar = False
    Application.DisplayStatusBar = Not Application.DisplayStatusBar
    ActiveWindow.DisplayWorkbookTabs = False
    ActiveWindow.DisplayHorizontalScrollBar = False
    ActiveWindow.DisplayVerticalScrollBar = False


    dWidth = ActiveWindow.Width
    dHeight = ActiveWindow.Height


'=======LOADING SCREEN=============='


    'Home Monitor '792 height '1452 width
    If dHeight = 792 And dWidth = 1452 Then
        ActiveWindow.Zoom = 106


    'Desktops '792 height '1452 width
    ElseIf dHeight = 769.5 And dWidth = 1272 Then
        ActiveWindow.Zoom = 100


    Else
    'Use Standard Default View Zoom
        ActiveWindow.Zoom = 106
    End If


End Sub


Private Sub Worksheet_Deactivate()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
    Application.DisplayFormulaBar = True
    Application.DisplayStatusBar = True
    ActiveWindow.DisplayWorkbookTabs = True
    ActiveWindow.DisplayHorizontalScrollBar = True
    ActiveWindow.DisplayVerticalScrollBar = True
End Sub
 
Upvote 0
The code runs for me when the workbook is opened.

Make sure you have events turned on.
Code:
Application.EnableEvents = True
 
Upvote 0
Hi - for some reason it doesn’t work for me

shall I add enable events at the start of the code and also an application.wait to ensure it captures the height/width?
 
Upvote 0
Hi - for some reason it doesn’t work for me

shall I add enable events at the start of the code and also an application.wait to ensure it captures the height/width?

If events are off then your code will not run as it is based on a event but events are off. You can type it in the immediate window of the VBA editor or in a module just have a sub that has the line of code. Run it to make sure events are on and then try opening your file and see if the open event runs.
 
Upvote 0
The event open definitely works because I’ve added in some more code at the end to update some cells with some values and that works and updates / it’s just the zoom part that it falls over on and I’m thinking is excel going to quick?

Would I need to add a wait and if yes which part shall I add it in?
 
Upvote 0
You can add a wait but I do not see how that would help. Your code runs for me without a wait.


it’s just the zoom part that it falls over on and I’m thinking is excel going to quick?

So the other lines run OK? the ribbon, and formula bar are hidden?
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top