将值添加到选项框和复选框

时间:2018-10-12 15:45:04

标签: excel vba excel-vba

我可以知道,如何像在列表框中一样选中选项框和复选框吗?让我们说,如果数据为是,则将自动选中该选项框,如果选择了Whatsapp和电子邮件,则将在WhatsApp和电子邮件中自动选中该选项框。

“方法”列从C9列开始,并在D9列参与。

仅供参考,

  

Emp 2-是Emp 3-否Emp 8-Whatsapp Emp 9-电话Emp 10-   Facebook Emp 11-电子邮件Emp 12-短信

enter image description here

这是我已经尝试过的编码

Private Sub lstEmployee_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

'dim the variables
Dim i As Integer
On Error Resume Next

'find the selected list item
i = Me.lstEmployee.ListIndex

'add the values to the text boxes
Dim methodsOfCommunication() As String

Me.Emp1.Value = Me.lstEmployee.Column(0, i)
Select Case Me.lstEmployee.Column(2, i)
    Case "Yes"
        Emp2.Value = True
        Emp3.Value = False
    Case "No"
        Emp2.Value = False
        Emp3.Value = True
End Select

' Reset Methods of Communication checkboxes.
Emp8.Value = False
Emp9.Value = False
Emp10.Value = False
Emp11.Value = False
Emp12.Value = False
' Set Methods of Communication checkboxes.
methodsOfCommunication = Split(Me.lstEmployee.Column(1, i), ", ")
For i = LBound(methodsOfCommunication, 1) To UBound(methodsOfCommunication, 1)
    Select Case methodsOfCommunication(i)
        Case "Whatsapp"
            Emp8.Value = True
        Case "Phone Call"
            Emp9.Value = True
        Case "Facebook"
            Emp10.Value = True
        Case "Email"
            Emp11.Value = True
        Case "SMS"
            Emp12.Value = True
    End Select
Next

Me.Emp4.Value = Me.lstEmployee.Column(3, i)
Me.Emp5.Value = Me.lstEmployee.Column(4, i)
Me.Emp6.Value = Me.lstEmployee.Column(5, i)
Me.Emp7.Value = Me.lstEmployee.Column(6, i)
Me.Emp13.Value = Me.lstEmployee.Column(7, i)
Me.Emp14.Value = Me.lstEmployee.Column(8, i)
Me.Emp15.Value = Me.lstEmployee.Column(9, i)

On Error GoTo 0

End Sub

1 个答案:

答案 0 :(得分:0)

Dim methodsOfCommunication() As String
Dim i As Integer

Me.Emp1.Value = Me.lstEmployee.Column(0, i)
Select Case Me.lstEmployee.Column(2, i)
    Case "Yes"
        Me.Emp2.Value = True
        Me.Emp3.Value = False
    Case "No"
        Me.Emp2.Value = False
        Me.Emp3.Value = True
End Select

' Reset Methods of Communication checkboxes.
Me.Emp8.Value = False
Me.Emp9.Value = False
Me.Emp10.Value = False
Me.Emp11.Value = False
Me.Emp12.Value = False
' Set Methods of Communication checkboxes.
methodsOfCommunication = Split(Me.lstEmployee.Column(1, i), ", ")
For i = LBound(methodsOfCommunication, 1) To UBound(methodsOfCommunication, 1)
    Select Case methodsOfCommunication(i)
        Case "Whatsapp"
            Me.Emp8.Value = True
        Case "Phone Call"
            Me.Emp9.Value = True
        Case "Facebook"
            Me.Emp10.Value = True
        Case "Email"
            Me.Emp11.Value = True
        Case "SMS"
            Me.Emp12.Value = True
    End Select
Next