You most certainly can call that code from any other module, but I discourage it. To do this, you must make sure it is declared as Public:
Public Sub CommandButton1_Click()
MsgBox "You pushed the button!"
End Sub
(If you create the code by double-clicking the button on the form designer, it will default to Private.)
This is not a good programming practice. Actions taken based on user events should be distinguished from programmatic actions. In the case where you want to share a function, then it is better to create a new common Sub and call that from the form and wherever else you want to use it.
This code goes in your UserForm module:
Public Sub CommandButton1_Click()
MsgBox "You pushed the button!"
DoSomething
End Sub
Then in a different Module, not the UserForm module:
Public Sub DoSomething()
MsgBox "This code is now Doing Something"
End Sub
And then from the other place you want to call it:
MsgBox "The code is doing this, not a user"
DoSomething
Bookmarks