I'm using the following simple code to run a macro "mymacro" when clicking/selecting a sheet.
Private Sub Worksheet_Activate()
Call mymacro
End sub
When i execute the macro manually it's working very well but when i click on the sheet it is not.
Basically i'm using the macro to change a chart colors...so when i apply this to a normal sheet where i have a chart as object it's working but when i tried with a sheet where there is only a chart (created by using "Move Chart" on new sheet option) nothing happens
Thank you for the help
The name needs to be Private Sub Chart_Activate() since it is a Chart and not a Worksheet. As VBasic2008 pointed out, the code needs to be in the code module for the Chart. Press Alt+F11 to open the VB Project, CRTL+R to open the Project Explorer. Double click on the Chart, eg.Chart1.
Your code module should look like :
Private Sub Chart_Activate()
Call mymacro
End Sub
Ensure that mymacro is Public or is also in this code module.
Related
I'm creating an excel module to export any table inside a workbook as .csv
My module generate a UserForm where the user can chose which table to export inside a combobox and then click 'export'
Everything is working perfectly, but I have to create a button in my workbook and call my module on click.
However I want to keep it very simple to setup inside any workbook by just import the module and run it with a keyboard shortcut '''Ctrl+R''' for example.
I search a lot about that but I never find something, Is it something possible?
Yes you can what you want to. First import your module to your new excel workbook and then assign keyboard shortcut to Workbook_Open event by copying code to ThisWorkbook area.
Sub MyMacro()
MsgBox ("ok")
End Sub
In ThisWorkbook area (which is in Microsoft Excel Objects / Project Window)
Private Sub Workbook_Open()
Application.MacroOptions Macro:="MyMacro", Description:="", ShortcutKey:= _
"a"
End Sub
This code assign Ctrl+"Defined_Letter" shortcut to your MyMacro macro. In this case, the code's shortcut is [Ctrl+a].
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 assigned a macro to a button in worksheet 2. I have created a new button in a new worksheet and now i want the same macro to run or call from worksheet 4.
...hmm....
Put your code in a module:
You can then call it from anywhere. If you were to add an ActiveX control to a sheet then you could just double click the button in design mode and amend the code to the following and it should work:
Private Sub CommandButton1_Click()
Call CallMeFromAnyWorkSheet
End Sub
I have created a macro and a button but I can't see where to edit the button setting to invoke the macro. I'm using Excel 2003.
If you've used a Form Control. The chance to assign a macro will come up right after you add the button to the sheet. Or, if the button is already on the sheet, you can right-click on it and select Assign Macro.
If you've used an ActiveXControl, right-click on the button and select View Code. This will open up the VBE with
Private Sub myCommandButtonName_Click()
End Sub
From this sub call your macro like so...
Private Sub myCommandButtonName_Click()
Call myMacro
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