VB code to run macro?

  • Thread starter Thread starter Tdp
  • Start date Start date
T

Tdp

I have a userform with a commandbutton1.
I have recorded a macro which I would to run it when commandbutton1 is
pressed.
 
Hi

Copy the desired code without Sub... / End Sub statements.

Select your userform in the VBA editor and right click on commandButton1,
select "view code". The code window for your userform will show:

Private Sub CommandButton1_Click()

End Sub

Paste your code between theese two lines.

Regards,
Per
 
Double click on the button and copy the recorded macro into it

Cheers
 
Sorry guys,
Its the other way round I'm after.
I have the code in the userform but I wish to move the code to a module,
name it macro1 and then in the userform code under commandbutton1 I want to
write a code to run macro1.
 
Hi,

If the sub routine MyMacro is in a regular module and is not Private, that
means its name is

Sub MyMacro()

not

Private Sub MyMacro()

Then the code attached to CommandButton1 in your user form is simpy

Private Sub CommandButton1_Click()
myMacro
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Run time error message???....Help!! 12
Error message 5
How to enumerate buttons on user form 2
VBA copy and paste data 2
Command Button Help 2
Delete duplicates? 2
Running code from userform 2
shared workbook 2

Back
Top