VBA macro

gdspeare

Board Regular
Joined
Oct 8, 2002
Messages
198
i understand how to record a macro in excel and how the macro is automatically written to vba. however, how do i create a macro directly from vba. if i have my user form created and the code written, how do i make it an active macro on a sheet?

thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
WELCOME TO THE BOARD!

In order to get your macro to run, you must have an EVENT trigger the macro. Once event is the ACTIVATE event. When your UserForm is activated, the macro will run. You will find several different events available for different objects.

To put your code in VBA, open the Visual Basic Editor. Double-click on your UserForm. Push the F7 key on your keyboard. You will see two pull-down menus on the right near the top. Select USERFORM in the first pull-down menu and select ACTIVATE in the second pull-down menu. You will now see something like this:

Private Sub UserForm_Activate()

End Sub

Put your code between the top line and End Sub. Did this help?
 
Upvote 0
let me be more specific:

i have a user form that has a text box and a command button - nothing else.

here is the simple code:

range("a1").value = textbox1.value

when i run it in the vba editor, no problems. but what do i have to do to attach this to a sheet?
 
Upvote 0
The trick is to have another macro in a Module or Sheet module "Show" your user form.

Like:

Sub myForm()
ActiveSheet.Select
UserForm1.Show
End Sub

Then use a Form Button, hot-key or Event to call the macro "myForm" in the code above, which will display your form. JSW
 
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top