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
Related
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.
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 a macro enabled workbook called import. When it opens a macro automatically runs; its main job is to import text files into each worksheet of a new workbook (this new workbook is called book1).
I have another macro that I required to run on book1 called runall.
Is it possible to configure Excel so that runall will then be activated when book1 is enabled?
Currently I have to go back into the original workbook called import and on the Developer tab activate runall.
What I have tried -
In the import workbook I have the following code:
Private Sub Workbook_Open()
Call CombineTextFiles
I have tried putting Call runall in the Private statement.
There are a lot of events you can tie your code to. One of them is Sheet Activate. In Book1 you can place the following code:
Private Sub Worksheet_Activate()
runall
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