如何从ComboBox填充文本框和选项按钮?

时间:2014-11-26 12:40:38

标签: excel-vba vba excel

当我从ComboBox中选择一些项目以填充文本框和我的“Sheet1”表中的选项按钮时,我需要这个用户表单的代码。

enter image description here

2 个答案:

答案 0 :(得分:1)

这样的事情会做你想要完成的事情。只需在将其他数据拉出的地方插入即可。

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

答案 1 :(得分:0)

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