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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
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?
 

gdspeare

Board Regular
Joined
Oct 8, 2002
Messages
198
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?
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
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
 

Forum statistics

Threads
1,144,151
Messages
5,722,801
Members
422,459
Latest member
Chriselff

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
Top