Create toggle Button Control with UserForm in Excel VBA

Welcome to Excel Avon

Toggle Button Control in UserForm

DOWNLOAD USED EXCEL FILE FROM HERE>>

In today’s post, we will tell you that How to Create Toggle Button Control in Excel VBA, as well as how to use the options of UserForm, which you have already read. In the previous post, we introduced you to Data Entry with Data Wizard. ToggleButton is used to show the enabled and unchecked states of a button.

It describes a control that is used to alternate between two or more states or options. When a toggle button is connected to a data source, it displays the current value of the data source as Yes/No, True/False, On/Off, or another pair of binary options. A toggle button is a control that performs one function at the first click at the first click, performs one function, and, upon the second click, another one. The button switches between the two of its modes, On and Off. When the button is chosen, it might have a value of True or False.

Create Toggle Button Control with UserForm

Well, you should know how to insert UserForm, as we have learned in many posts till now, you can insert UserForm. Let us teach you how we insert the UserForm. Let’s understand, then we have to go like last time, first go to the Developer Tab, then click on the option of Visual Basic as shown in the image below.

sort-data-on-excel-using-VBA

On opening in VBE, you have to go to Insert and then UserForm has to be inserted, as can be seen in the image.

introduction-of-userform

When we click on the User form, a separate wizard box will be created in a window VBE, in which you can see UserForm1 as the default name. You can increase or decrease the size of the UserForm with the mouse pointer.

Data-entry-with-wizardbox

Now we will insert Toggle Button from toolbox.

Toggle Button Control

Toggle button control has been inserted, its default name will be toggle button Control.

Toggle Button Control.png

Now we will click Run button.

Toggle Button Control1

If you click the button the first time, the setting is on. If you click the button a second time, the setting is turned off.

Toggle Button Control2

Back we will go to VBE where we will right click in Userform then click in view code then create code for toggle button.

Toggle Button Control-3

Write script for toggle button, First we’ll create the condition. If value of togglebutton in userform is true then the caption of the button will be ‘ON’. Else the caption of the button will be OFF.

Private Sub ToggleButton1_Click()
If Me.ToggleButton1.Value = True Then
    Me.ToggleButton1.Caption = "ON"
Else
    Me.ToggleButton1.Caption = "OFF"
End If
End Sub

Now we will click Run button.

Toggle Button Control1

When we click the button first time, the setting of the button will be On, but if we click the button again, the condition of the button will be off. We can turn on or off the button setting by clicking on the button.

 Create Toggle Button Control with UserFormCreate Toggle Button Control with UserForm1

Now we will close the Userform, go back to the view code and now we will use the color function for true and false.

Private Sub ToggleButton1_Click()
If Me.ToggleButton1.Value = True Then
    Me.ToggleButton1.Caption = "ON"
    Me.ToggleButton1.BackColor = vbRed
Else
    Me.ToggleButton1.Caption = "OFF"
    Me.ToggleButton1.BackColor = vbgreen
End If
End Sub

Now we will click Run button.

Toggle Button Control1

When we click the button for the first time, the setting of the button is set to ON, and the color of the button will be red, but if we click the button again, the setting of the button will be OFF. And the color of the button will turn green, you can turn on or off the button setting by clicking on the button.

Create Toggle Button Control with UserForm2 

Now close Togglebutton form and by going to the properties of the toggle button when the triple state property is true, the user can choose between the values of null, true, and false.

Therefore, I hope that you have understood Create Toggle Button Control with UserForm in Excel VBA, maybe if you do not understand some options, then you can comment us, which we will answer soon and for more information, you can follow us on Twitter, Instagram, LinkedIn and you can also follow on YouTube.

 

LEARN MORE TOPIC IN VBA HERE

DOWNLOAD USED EXCEL FILE FROM HERE>>

You can also see well-explained video here about Create Toggle Button Control with UserForm in Excel VBA

 

Leave a Reply