简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM