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!
You Might Also Like
To view this protected post, enter the password below:
If you've ever encountered a corrupt Excel workbook, you've probably tried lots of things already, including possibly tearing your hair out. In this particular scenario, we have lots of extra sheets that show up in the Visual Basic Editor. This is not great. ...and it...
Sometimes in Excel, we need to automate simple tasks, like to create sheets automatically for each item in a range. If you just had a few worksheet tabs that needed to be created, it wouldn't be a big deal, but what if you had to create 100 different worksheet tabs?!...
Date Picker Calendar in Excel VBA Oftentimes, users want to click a button and select a date. This is no different for Excel developers. Check out this ActiveX control by Microsoft that allows users to do just that. It's a little old school looking, but actually has...
Check out how Jiyaad is able to save 5+ hours biweekly! He's using the Complete Web Automation with Excel VBA and Selenium course, jam packed with 8+ hours of videos.Want to see Free Previews? Click here.Get Lifetime Access to the course by using coupon code 'EVBAJYD'...
In order to highlight text in a TextBox (other than Tabbing into that field), you can insert a couple helpful snippets of VBA code that will help you accomplish this easily! The first line tells VBA to put the cursor at the leftmost (0) character:...