开发者

Attach Existing UserForm Button to Excel Workbook Sheet

开发者 https://www.devze.com 2023-04-10 23:27 出处:网络
I have an Excel workbook that has a macro (the only macro in the workbook) attached to a button on a 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/User开发者_如何转开发Form 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号