0
votes

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.

2

2 Answers

1
votes

Create one macro for example

Sub ShowForm
    YourForm.show
End Sub

And associate this macro in button.

1
votes

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