Computer Hope
Software => Computer software => Topic started by: alex1991 on February 20, 2008, 05:06:41 AM
-
Does anyone know how to apply a macro to a button on a userform in Microsoft Excel using VB? Response needed within the next 24 hours if possible.
-
Does anyone know how to apply a macro to a button on a userform in Microsoft Excel using VBA?
At first I thought this was a Yes or No question. ;D
When you double click your button in design view, the VB Editor will pop up. Write your macro within the Sub/End Sub statements already provided. This is the click event for the button.
This works for any control on the form, including the form itself. The default is the click event (upper right hand corner) but you can change it as needed.
8)
-
Thanks, but if I wanted to make a button called Quote run a macro of the same name, what code would I need to type in?
-
Are you drawing this button on the worksheet? Or do you want to attach a macro to a toolbar button?
For the former, the original response will work. Draw the button. You can right click the control to change properties such as the caption, name, etc. When you double click the button the VB Editor will appear and you can write your code. Once written you can use Tools-->Macro-->Macros to edit or even assign a shortcut key sequence (options button)
For the latter, go to Tools-->Macro-->Visual Basic Editor. In the left pane either double click the sheet name (local to the sheet) or thisworkbook (local to the workbook). Again the Visual Basic Editor will appear and you can write your code. Attaching the code to a button is more problematic. I suggest you use the help and search for "Add a button" for a detailed description.
Hope this helps. 8)
-
Thanks, the buttons are working now. Much aprreciated.
-
Found a different way to make the buttons work. If you copy the code from the macros into the code for the buttons then the buttons will perform all of the functions that the macro does.