I'm trying to automatically display the IDE when Excel is launched.
Is there a way to simulate a click on the "Visual Basic Editor" icon in the ribbon? I looked into Application.CommandBars
but there's nothing about the Ribbon.
Private Sub Workbook_Open()
' Display Visual Basic Editor
End Sub
Excel Event code makes this possible and it is easier than you think. Event code can run a macro based on a drop-down list, event code is VBA code stored in a worksheet module or workbook module.
Using Auto open method to run a macro automatically: Insert a New Module from Insert Menu. Copy the above code and Paste in the code window. Save the file as macro enabled workbook. Open the workbook to test it, it will Run a Macro Automatically.
Code to Open the Form Automatically To make the UserForm open automatically, you'll add a bit of code to the workbook's code module, in the Workbook_Open procedure. To add the code: In the UserForm workbook, press Alt + F11, to open the Visual Basic Editor.
The Commandbars
object has an ExecuteMso
method that allows you to "push" any Ribbon button, so:
Application.CommandBars.ExecuteMso ("VisualBasic")
As noted by Comintern, the Application
qualification is necessary when using this in a Workbook_Open
event, otherwise you'll get an error 91.
To find the mso, go into the Quick Access toolbar's Ribbon menu, find what you want and hover:
It would be
Private Sub Workbook_Open()
' Display Visual Basic Editor
Application.VBE.MainWindow.Visible = True
End Sub
If you get Programmatic Access error: Programmatic Access To Visual Basic Project Is Not Trusted - Excel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With