Workbook_open() not working because of timing

longtran295

New Member
Joined
Sep 13, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Dear all,
I write some code to expand ribbon when open workbook (in ThisWorkbook):
VBA Code:
Private Sub Workbook_open()
If Application.CommandBars("Ribbon").Height<=100 Then
SendKeys "^{F1}", True
End If
End Sub
But it not working. I think because of loading time when file opened.
Could someone has solution for this issue?
Many thanks!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Following the logic of your code, it will only execute if the height of the Ribbon is equal to or less than 100 pixels. On my system, the ribbon (in its collapsed state) is 143 pixels. That means your code would never actually execute on my system. I suspect that may be the issue with your computer too. Try changing the 100 to 150 pixels and see if that works?
 
Upvote 0
If you do want run a macro shortly after the workbook has opened you can use this technique:
put this macro in the workbook open event:
VBA Code:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:15"), "delaycall"
End Sub
the in a standard module put this code:
VBA Code:
Sub delaycall()
If Application.CommandBars("Ribbon").Height <= 100 Then
SendKeys "^{F1}", True
End If
MsgBox (" open delay run")

End Sub
 
Upvote 0
The solution posted by @offthelip is a useful one, and common for trying to make sure that code will run upon opening a workbook. What you can do is try adjusting the period of time to see what does/does not work. At present the code waits 15 seconds before running the subroutine - perhaps try experimenting with a shorter time frame (1 second?) to see if it will work almost at exactly the same time as the workbook is opened.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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