Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically display the IDE when a workbook is opened

Tags:

excel

vba

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
like image 948
michael Avatar asked Aug 04 '16 01:08

michael


People also ask

Which event runs macro automatically when a workbook is opened?

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.

Can you make a macro run automatically when Excel opens?

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.

How do I make an Excel document open 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.


2 Answers

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:

enter image description here

like image 88
Doug Glancy Avatar answered Sep 20 '22 23:09

Doug Glancy


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

like image 25
Oscar Ortiz Avatar answered Sep 19 '22 23:09

Oscar Ortiz