Label linked to a text box value

2.6k Views Asked by At

Good morning, I am editing an User Form on VBA Excel and I would like to show an alert if the user insert a certain value in a text box. I wrote this code:

If txtbox.Value < 0 Then
lbl_Alert.Visible= True
Else
lbl_alert.Visible=False
End IF

The code works properly but once the alert appears if I change the value in the text box it doesn't disappear. What string should I add to my script to let the label disappear when I change the text box value?

Thank you!

2

There are 2 best solutions below

0
On BEST ANSWER

put your code under the textbox_change event

Following works fine

Private Sub TextBox1_Change()
    If Me.TextBox1.Value < 0 Then
        Me.Label1.Visible = True
    Else
        Me.Label1.Visible = False
    End If
End Sub
0
On

First create a sub (into your UserForm module, or in a regular module but with the Public declaration), like this :

Public Sub AlertDisplay()
    If UserForm1.txtbox.Value < 0 Then
        UserForm1.lbl_alert.Visible = True
    Else
        UserForm1.lbl_alert.Visible = False
    End If
End Sub

And then call that sub into your Private Sub TextBox_Change (double click on that textbox to access it easily), just by adding a line : AlertDisplay