How to hide ribbon with on event macro in excel? - excel

I have been trying to hide a ribbon automatically when opening the workbook with this code
Private Sub Workbook_Open()
Application.CommandBars.ExecuteMso "HideRibbon"
End Sub
It is not working as on-event open workbook but it is working when i press F5, any advice?
'

Related

Displaying Excel Userform without opening file

I have created a Userform, currently I am using this code to hide Excel and just show the userform (Code was place in Workbook)
Private Sub Workbook_Open()
Application.Visible = False
ricwin.Show
End Sub
The issue I am facing is that in ocassions the excel file will open but not as macro enable, and I require to close it and reopen it so the macro runs.
Also is there a chance to display a taskbar icon for the userform without an excel file being in background?
You can fix the problem by adding a ToggleButton to the userform. When the workbook is opened, the worksheet is hidden, only the userform is shown. For this, the following codes are added to a module:
Sub Auto_Open()
Application.Visible = False
UserForm1.Show
UserForm1.ToggleButton1.Value = False
End Sub
enter image description here
Source of sample file

Excel Macros run but don't show in Developer Tab

I created a spreadsheet in the past which contained buttons that would run macros in the workbook. When I open the spreadsheet now and press the button, the macro will run perfectly. However, on the Developer tab, there are no macros listed and no code available. Is there a way to make the macros visible?
I do not understand. Is that your problem?
Option Explicit
Option Base 1
' This Macro is shown.
Public Sub X()
MsgBox ("X")
End Sub
' This Macro is hidden.
Public Sub Y(ByVal N As Long)
MsgBox ("Y")
End Sub

Automatically Run VBA Code when an Excel Workbook Opens

I have VBA code in I would like to run when the Excel workbook is opened.
I tried creating a public procedure in the sheet the code is supposed to run in:
Public Sub Workbook_Open
' Some code here
End Sub
It does not run when the workbook opens.
It is supposed to create a combobox in one of the cells and then fill it with information from the database.
Make sure that the code is in the ThisWorkbook scope of the VBA editor and not in a module or worksheet:
Option Explicit
Private Sub Workbook_Open()
MsgBox "Autorun works!"
'your code here
End Sub
And make sure that your macros are enabled.
For details also see Microsoft's documentation: Automatically run a macro when opening a workbook.
Adding to #Pᴇʜ's answer, you can also use the following procedure in standard module:
Sub Auto_Open()
'// Your code here...
End Sub
You are trying to create an event procedure that activates when you open a book. Go to thisworkbook in the VBA editor and choose workbook open procedure from the drop down list above the coding window, or you can type manually:
Private Sub Workbook_Open()
End Sub

Excel VBA On Open Macro Runs for other Workbooks

I have a very simple Workbook_Open procedure that hides the ribbon. Problem is if I have that workbook with the macro open and then open a another different Excel workbook the procedure runs for that workbook and hides its ribbon as well (even for Excel 2003 files)
Any ideas on what is going on? Below is the procedure
Private Sub Workbook_Open()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
End Sub
Answer stolen from this mrexcel topic.
You need to add Private Sub Workbook_Deactivate and Private Sub Workbook_Activate to ThisWorkbook as well:
Private Sub Workbook_Activate()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
End Sub
Private Sub Workbook_Deactivate()
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
End Sub
When the new workbook is opened, the previous one will be deactivated, and show the ribbon for the new workbook.
Tested working on Excel 2013

Attach Existing UserForm Button to Excel Workbook Sheet

I have an Excel workbook that has a macro (the only macro in the workbook) attached to a button on a sheet.
In VB mode, I created a UserForm under Forms with a CommandButton1_Click Sub and when run from within VB (Run > Run Sub/UserForm or F5) it runs fine. I have it calling a Shell command that runs a BAT file that runs a Python script.
How do I run CommandButton1_Click from a button on a sheet? If I try to add a button to a sheet, it offers me the macro I have already associated with the other button.
Create one macro for example
Sub ShowForm
YourForm.show
End Sub
And associate this macro in button.
Why don't you move the main code into a new macro to modularise it. You can then call your macro via both UserForm and worksheet ActiveX (or Forms) buttons
'Normal Code Module
Sub TestCode()
MsgBox "Hi"
End Sub
'UserForm code
Private Sub CommandButton1_Click()
Call TestCode
End Sub
'ActiveX button code
Private Sub CommandButton1_Click()
Call TestCode
End Sub

Resources