SIH007
SIH007

Reputation: 31

Get the value of a combobox using ListIndex in a excel vba userform

I created an Excel Userform with a comboBox. When the user changes the selected item of the combo the data entered in the textboxes will be cleared. So before that happens I want to give him the warning: if you change the value of the combo from Item1 to Item2, all your text will be cleared.

I have the listIndexes of the combo before and after the change but is there a possibility to get the according value? I already tried

strValueOld = UserForm.cboSelection(intIndexOld).value 

and similar stuff, but nothing works. Would be great if you knew how to achieve that.

Upvotes: 0

Views: 45

Answers (1)

taller
taller

Reputation: 18943

Assuming the controls on the UserForm are named TextBox1 and ComboBox1.

Option Explicit
Dim strCmb As String

Private Sub ComboBox1_Change()
    With Me.ComboBox1
        If Len(strCmb) > 0 Then
            MsgBox "User selection changed: " & strCmb & " >> " & .Value & _
                vbCrLf & "TextBox will be cleared."
            Me.TextBox1.Text = Empty
        End If
        strCmb = .Value
    End With
End Sub

Private Sub CommandButton1_Click()
    Unload Me
End Sub

enter image description here

Upvotes: 1

Related Questions