Thursday, July 30, 2009

How do you run a macro from a command button in Visual Basic?

I'm trying to create a command button in an Excel spreadsheet.


I have the code screen in front of me, but I don't know how to tell it to run a macro I've already recorded.





Thanks for your help! Important project!

How do you run a macro from a command button in Visual Basic?
when you clicked on "create new macro" the first screen that pops up asks you to chose a key which will be used with the control button. after youve recorded the macro, hit control with whatever key you selected and it should work. if it doesnt, you may have something messed up in your spreadsheet, like merged cells where they should be separate in order to perform the macro.
Reply:Go to command buttons and then go to Customize. Proceed to Macros and there is a custom macro button. Drag it to the sheet and then you can customize it to the macro you want it to run.
Reply:Go to View - toolbars in the menu and turn on the "control toolbox." click on the command button or other control you want to use and then click in the work sheet where you want it.





Right click on the button and select Format. Enter the text you want to display. click off of the object and then right click again. this time select "properties." Set the properties as desired.





Open the VBA editor from the menu, select the sheet that contains the button, select the button from the drop down on the top of the window. Now code the "clicked" event (or whatever event you want to) to call the macro you've already writen. For example, if your macro is called fooBar you would include the line fooBar or fooBar [parameter list] in the code immediately after the "Private Sub CommandButton1_Click()" and before the line "end sub"


No comments:

Post a Comment