Search code examples
excelvbalabeluserform

Can we click multiple times on a specific label we have created and change the label text each time we click (in user form of excel VBA)?


I have a user form in excel written in VBA. There is a label in my user form and I want to change this label's caption each time I click on it. For example: This label currently have "1" caption, for first time I click on this label, I want to change the label's caption to "X", for the second time I click on the label, to change the label's caption to "?", and If I click one more time, I want to change to first caption which was "1". I prefer using labels in this scenario, and Unfortunately other options like ComboBox or OptionButtons are not suitable for me. Is there any way to this scenario? Thanks in advance for any advice.

I just wrote this part of the code for my label and didn't know how to write the rest:

Private Sub ExtUR_Label1_Click()
    ExtUR_Label1.Caption = "X"
    ExtUR_Label1.ForeColor = &H8000000D
End Sub

Solution

  • You have the right idea. Something like the following should do what you need:

    Private Sub ExtUR_Label1_Click()
       If ExtUR_Label1.Caption = "1" Then
          ExtUR_Label1.Caption = "X"
       ElseIf ExtUR_Label1.Caption = "X" Then
          ExtUR_Label1.Caption = "?"
       Else
          ExtUR_Label1.Caption = "1"
       End If
    End Sub