Attach Existing UserForm Button to Excel Workbook Sheet - excel

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

Related

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

Running Excel Macro (Button Click) using Bat script

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.

How to call a macro from another worksheet.?

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

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

Resources