根据ListBox选择更改标签标题

时间:2016-09-28 15:15:45

标签: vba excel-vba listbox label excel

根据用户在ListBox中选择的内容,更改标签名称的最佳方法是什么。 到目前为止,我有这个:

Private Sub Label6_Click()
    Dim lItem As Long
    For lItem = 0 To ListBox1.ListIndex <> -1
        If ListBox1.Selected(lItem) = "AAA" Or "BBB" Then
            Me.Label6.Caption = "Select Graphite"
        Else
            Me.Label6.Caption = "Select Oil System"
        End If
    Next lItem
End Sub

不幸的是它不起作用,我错过了什么? 谢谢!

1 个答案:

答案 0 :(得分:1)

除非你的列表框是Multiselect,否则你不需要遍历它:

Private Sub Label6_Click()
    Dim lItem As Long
    lItem = ListBox1.ListIndex
    If lItem <> -1 then
        Select Case ListBox1.List(lItem)
            Case "AAA", "BBB"
                Me.Label6.Caption = "Select Graphite"
            Case Else
                Me.Label6.Caption = "Select Oil System"
        End Select
    End If
End Sub