VBA Learner: Controls of a User form

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,584
Office Version
  1. 365
Platform
  1. Windows
While learning the Userform, I was confused about the usage of the below mentioned controls.

Do these have the same usage where it is just a preference of user to use any one of them or do they have a particular scenario when one should be used and other not.

1. Toggle Button
2. Option Button
3. Combo Box
4. List Box
5. Check Box

I am a learner in Userform and with my limited knowledge I guess the below:

Option button would be either two or more so it takes more space, hence we can use one Toggle button instead when it comes to lack of space. E.g. two option buttons for Gender (Male and female) whereas a single Toggle Button can be used wherein it toggles on clicking. (Option button and Check box seems to have the same usage?)

List Box: the only one from the above that can take multiple selections. in case of one selection

Combo Box: is used when the source data is too long to select from and hence a combo box dropdown is used. Toggle or option Button wouldn't be possibility as it will take a lot of space and add to clumsiness.

Please guide me if I am in the correct direction.
Also, in case I am missing some other tool related to this, additional guidance is welcomed and appreciated.

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Well all these different controls have unique purposes
Here are two examples.

With Option Buttons only one of them can be chosen unless in different Frames
And a Toggle Button can only be True or False:

So lets say your selling dresses and the byer can choose between three different colors but can only choose one of them. So you would use several Option Buttons with the caption being different on each option button. Like Red White Blue
So in my example the color chosen would be put in Textbox1

And there are several different but unique uses for each control.
VBA Code:
Private Sub CommandButton1_Click()
'Modified 3/18/2020 1:43:39 PM EST
'When using Option Buttons. Only one can be selected
For Each xcontrol In Me.Controls
If TypeName(xcontrol) = "OptionButton" Then
If xcontrol.Value = True Then TextBox1.Value = xcontrol.Caption
End If
        Next xcontrol
End Sub
Private Sub ToggleButton1_Click()
'Modified 3/18/2020 1:43:39 PM EST
'If using Toggle Button
If ToggleButton1 = True Then TextBox1.Value = "Pie"
If ToggleButton1 = False Then TextBox1.Value = "Cake"
End Sub
 
Upvote 0
Thanks My Aswer Is This. Appreciate your help.

I did a bit of reading and found that "Checkbox" and "List box" have the same usage.
Am I correct?
 
Upvote 0
No none of these controls are exactly the same.

A Checkbox is either checked or not checked. So it's considered as true or False

A listbox is a control where you can Load in all the value in A range on your Worksheet.

Lets say you want all the values in Range(A1To A24) to be seen in your listbox

You could use code to do this

Or you can just load values into the listbox by writing code to add what ever values you want into the listbox


Sort of like you have a list of 100 items you have for sale

Load all those values into the listbox.
The byer can then choose which item in the list they want to buy.
And with coding they could choose more then one

This is sort of like a ComboBox
But with Combobox it's normally a small control and you scroll down to make a choice
A listbox I would think would only be used if you only have 10 items for sale.
You make the listbox height the way you want and user can see all values without scrolling down.

But now I have only pointed out some different particulaities. I'm sure there are more.

If your not use to using Excel or Excel Userforms.
I suggest starting out with small projects till you see how things work out.
Show us your code and we may have other ways that may work easier for you.
If you find yourself writing a hundred lines of code to do a job.
You may want to ask for help and we may be able to show you a way to do things with less need for code.
I would say most of the code I write is less then 50 lines of code.
 
Upvote 0
Thank you for your detailed explanation mate. I am going to start creating and writing small codes for UserForms as and when I get time. That certainly seems to be the best way to remember stuff.
 
Upvote 0
Thank you for your detailed explanation mate. I am going to start creating and writing small codes for UserForms as and when I get time. That certainly seems to be the best way to remember stuff.
Try reading code you see on this forum and see if you understand the code. Some code like Range("A1").value="Cake". Seems simple to me. But their are several ways to do that. Like

VBA Code:
Sub Diffent_Ways_To_Do_Things()
'Modified 3/18/2020 2:58:31 PM EST
Range("A1").Value = "One"
Cells(2, 1).Value = "Two"
[A3].Value = "Three"
MsgBox "Have a Nice Day"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,941
Members
449,094
Latest member
teemeren

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
Back
Top