Can't change Visible property in Label of a VBA UserForm

5.3k views Asked by At

I've got a Excel form named UserForm1 with a label with this properties: Label6 properties

This label has the property Visible=False. I want to make visible the label when the user click the CommandButton1, for that I wrote the next code:

Private Sub CommandButton1_Click()
    Me.Label6.Visible = True
    Dim oficina_garsa, file_source, file_solds As String
    Dim invoice_year, invoice_month As Integer
    oficina_garsa = TextBox3.Value
    file_source = TextBox1.Value
    file_solds = TextBox2.Value
    invoice_year = CInt(ComboBox1.Value)
    invoice_month = ComboBox2.ListIndex
    Debug.Print oficina_garsa, file_source, file_solds, invoice_year, invoice_month
    Call MainProcess(oficina_garsa, file_source, file_solds, invoice_year, invoice_month)
End Sub

But it doesn't work. So I wrote other event code linked to CommandButton2 to testing purpose like this:

Private Sub CommandButton2_Click()
    If Me.Label6.Visible = False Then
        Me.Label6.Visible = True
    Else
        Me.Label6.Visible = False
    End If
    Application.Wait Now + TimeValue("00:00:03")
End Sub

And the last code works fine while the first one doesn't! But in the first code the next code after Me.Label6.Visible = True runs whole without error message.

I've tried replacing 'Me' by 'UserForm1', but the result is the same. Why assigning the Visible property to True works in the CommandButton2 event but doesn't in the CommandButton1 one?

1

There are 1 answers

0
Simon On

I know this thread is quite old, but as I came across it with the same problem, I thought I'd post the fix.

After setting the visible argument, add a line

Me.Repaint

This will re-render the form and update the visibility of the element. I think the Repaint command is specific to User Forms, so not sure if this will work in other situations where elements are not rendering accurately.