Excel VBA UserForms #2
Create a UserForm with 2 option buttons: 1 Sort by Employee ID or 2 Sort by Name.
Part one, create the UserForm and give it buttons.
- First, bring up the VBA UserForm editor (F11), and add a new UserForm using the dropdown in the upper left of the screen.
- Rename the UserForm to "sortform" and change the caption to "Choose sort option":
- Add two Option Buttons (or as many as you need for your form), and two Command Buttons to your UserForm using the toolbox:
- Next, (name) and Caption your command buttons (sort and cancel, respectively).
- Then let's program the Cancel button, using the .hide command from UserForms #1 video.
- Now we'll edit the caption on the Option buttons, naming one Emp Id, and the other Name.
Part Two, recording a macro
- Click the Record macro button, hit ok on the window that pops up, and select your range of cells you wish to include for sorting
- Select Data on the menu bar and hit the sort button, then select Emp Id in the Sort by dropdown box and hit ok. Repeat for the Name column.
- Bring up the VBA window with alt-F11
Part three, use the macro to code the buttons
- Go to the most recent module and copy the selection as shown
- Select the sortform and double-click the sort button to paste in the code
- Add in the following code above your pasted code:
- Add in this code, or for step-by-step instructions and complete explanation, please watch the full video below!