s.s.
s.s.

Reputation: 71

How to fill textboxes and optionbutton from ComboBox?

I need a code for this userform, when I choose some item from ComboBox to fill textboxes and optionbutton from my "Sheet1" table.

enter image description here

Upvotes: 1

Views: 1288

Answers (2)

s.s.
s.s.

Reputation: 71

This is the code for this userform:

Dim Project As Workbook
Dim SheeT As Worksheet
Dim Closing As Boolean

Private Sub UserForm_Initialize()

Set Project = Workbooks("Navi4.xlsm") ' ThisWorkbook
Set SheeT = Project.Worksheets("Sheet1")

With SheeT
    ComboBox1.List = .Range("C2", .Range("C" & Rows.Count).End(xlUp)).Value
End With

End Sub

Private Sub ComboBox1_Change()

Dim Item As Long

Item = ComboBox1.ListIndex

If Item = -1 Then
    Exit Sub    ' nothing selected
End If

TextBox1.Value = SheeT.Range("C" & Item + 2).Value
TextBox2.Value = SheeT.Range("D" & Item + 2).Value

If Sheets("Sheet1").Range("E" & Item + 2).Value = "Male" Then
    OptionButton1 = True
    OptionButton2 = False
ElseIf Sheets("Sheet1").Range("E" & Item + 2).Value = "Female" Then
    OptionButton1 = False
    OptionButton2 = True
Else
    MsgBox ("Sex not Male or Female")
End If

End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

Closing = True

End Sub

Upvotes: 0

peege
peege

Reputation: 2477

Something like this would do what you are trying to accomplish. Just insert this wherever you are pulling the other data out.

If Sheets("SheetName").Cells(row, 5) = "Male" Then
    OptionButton1 = True
    OptionButton2 = False
ElseIf Sheets("SheetName").Cells(row, 5) = "Female" Then
    OptionButton1 = False    
    OptionButton2 = True
Else
    MsgBox("Sex not Male or Female")
End If

Upvotes: 1

Related Questions