Running Excel Macro (Button Click) using Bat script - excel

I have written an excel Macro, that runs on a button click in Excel.
I want to run the same macro using bat script, i.e. I want to open the excel and click the Button using bat scripting.
I have succeeded in opening the excel sheet but I am unable to run the VBA code(i.e. Click the Button)
I have written the VBA code in following function
Private Sub CommandButton1_Click()
...
...
End Sub
I have tried the following code for bat script-
Start G:\Excel\test.xlsm
Run(CommandButton1_Click)
Pause
Thanking In anticipation

I would recommend that you just add the following Sub:
Private Sub Workbook_Open()
Call CommandButton1_Click
End Sub
This will run your CommandButton1_Click as soon as the workbook opens.

Related

Calling a macro when selecting a chart sheet

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.

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

How can I tie a VBA macro to a button in Excel

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

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