Search code examples
excelvbaradio-buttonuserform

How to hide option button on Userform if the caption has no text


I have 4 option buttons and their caption is set to different cells. However, sometimes that cell value doesn't contain any text, and I want to hide the option button from the Userform if that is the case. But my code is hiding the option buttons even if the caption contains text. I'm sure this is simple, but I can't solve it.

Call ifBlank

OptionButton1.Caption = qRange1.Value
OptionButton2.Caption = qRange2.Value
OptionButton3.Caption = qRange3.Value
OptionButton4.Caption = qRange4.Value

Sub ifBlank()


If OptionButton3.Caption = "" Then

    OptionButton3.Visible = False

If OptionButton4.Caption = "" Then

    OptionButton4.Visible = False
    
End If
End If

Solution

  • a) Call ifBlank after setting the captions, not before.

    b) You can simply write

    Sub ifBlank()
        OptionButton1.Visible = (OptionButton1.Caption <> "")
        OptionButton2.Visible = (OptionButton2.Caption <> "")
        OptionButton3.Visible = (OptionButton3.Caption <> "")
        OptionButton4.Visible = (OptionButton4.Caption <> "")
    End If