ActiveX controls – Option Button in Excel VBA

Building an application for entering data – Inserting an Option Button Option (ActiveX Control)

We would like the data on gender to be entered in the “I” column of our data table, which is the value of Woman or Man. In this case, we should construct our application in such a way as to make it impossible to mark both options at the same time. The best way to do this is to use it OptionButton. How do I insert an OptionButton? From the ActiveX controls in the Developer tab, select two Options button controls. Put them above the column “I”. When entering the properties of controls, let’s correct for them the values ​​in the Caption fields for “Woman” for OptionButton1 and “Male” for OptionButton2. It is worth noting that another common name of the radio button is RadioButton


Building an application for entering data – Programming the Option Button Option (ActiveX Control)

OptionButton buttons are already embedded in our application. How can we program OptionButton option buttons? In the code block responsible for entering the data, we should enter the conditional statement If Then Else . Below the code snippet:

If OptionButton1 = True Then
Cells(sngWiersz, 9) = "Woman"
ElseIf OptionButton2 = True Then
Cells(sngWiersz, 9) = "Men"
Else: Cells(sngWiersz, 9) = ""
End If

As you can see, we have introduced the ElseIf element to our instruction . The code has been saved in this way, because we can meet with the situation when no option is selected. The selected radio button accepts True in VBA . Decorated False value . The code of our program in the Arkusz1 object now looks like:

Private Sub CommandButton1_Click()

Dim sngId As Single
Dim sngWiersz As Single
Dim sngLoginLicznik As Single

sngId = 1 + Application.WorksheetFunction.Max(Range("A:A"))
sngWiersz = 6 + Application.WorksheetFunction.CountA(Range("A:A"))

sngLoginLicznik = 7

Do While sngLoginLicznik <= sngWiersz

If Cells(sngLoginLicznik, 2) = LCase(TextBox1) Then
MsgBox "Ten login znajduje się już w bazie, wprowadź inny", vbCritical
GoTo endlabel
End If

sngLoginLicznik = sngLoginLicznik + 1

Cells(sngWiersz, 1) = sngId
Cells(sngWiersz, 2) = LCase(TextBox1)
Cells(sngWiersz, 3) = UCase(Left(TextBox2, 1)) & LCase(Mid(TextBox2, 2))
Cells(sngWiersz, 4) = StrConv(TextBox3, vbProperCase)
Cells(sngWiersz, 5) = LCase(TextBox4)
Cells(sngWiersz, 6) = ComboBox1
Cells(sngWiersz, 7) = DateSerial(ComboBox4, ComboBox3, ComboBox2)
Cells(sngWiersz, 8) = ListBox1

If OptionButton1 = True Then
Cells(sngWiersz, 9) = "Kobieta"
ElseIf OptionButton2 = True Then
Cells(sngWiersz, 9) = "Mężczyzna"
Else: Cells(sngWiersz, 9) = ""
End If
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: