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
Related
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
I am using Excel 2016 and have this code written in the ThisWorkbook object in VBA:
Private Sub Workbook_Open()
ThisWorkbook.Protect (password = "password")
End Sub
It is not working. The point here is that this should prevent users from touching the Power Query functions in this workbook. I have saved it as a Macro-enabled workbook, I have all macros and events enabled and this is the only workbook open.
I also have this additional code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Protect (password = "password")
ThisWorkbook.Save
End Sub
And that is not working either. It works fine if I insert that "ThisWorkbook.Protect" code into a general module or worksheet object and run it manually, but when I want this particular excel file to run this code automatically on open or close, it does not do it.
Any ideas what could be causing this?
For some reason running ThisWorkbook.Protect on a protected workbook seems to unprotect it (even though I couldn't find any documentation that says that it does this) so try this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ThisWorkbook.ProtectStructure Then ThisWorkbook.Protect Password:="password"
ThisWorkbook.Save
End Sub
In order for automatically-running subroutines to work correctly in Microsoft Excel, they must be contained within a Visual Basic module.
If an Auto_Open, Auto_Close, or other automatically-running subroutine is stored "behind" a worksheet or ThisWorkbook, it may not function correctly when you open or close your workbook, or when you perform an action that should cause the subroutine to run.
MS Topic Discussion - Code "Behind" a Workbook
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?
'
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
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