Microsoft Office Tutorials and References
In Depth Information
Understanding UserForm Events
Private Sub TextBox1_Change()
NewVal = Val(TextBox1.Text)
If NewVal >= SpinButton1.Min And _
NewVal <= SpinButton1.Max Then _
SpinButton1.Value = NewVal
This procedure starts by using VBA’s Val function to convert the text in the TextBox to a value.
(If the TextBox contains non-numeric text, the Val function returns 0 .) The next statement
determines whether the value is within the proper range for the SpinButton. If so, the
SpinButton’s Value property is set to the value entered in the TextBox.
About the Tag property
Every UserForm and control has a Tag property. This property doesn’t represent anything
specific, and, by default, is empty. You can use the Tag property to store information for your
For example, you may have a series of TextBox controls in a UserForm. The user may be
required to enter text into some but not all of them. You can use the Tag property to identify
(for your own use) which fields are required. In this case, you can set the Tag property to a
string such as Required . Then when you write code to validate the user’s entries, you can refer
to the Tag property.
The following example is a function that examines all TextBox controls on UserForm1 and
returns the number of required TextBox controls that are empty:
Dim ctl As Control
For Each ctl In UserForm1.Controls
If TypeName(ctl) = “TextBox” Then
If ctl.Tag = “Required” Then
If ctl.Text = “” Then
EmptyCount = EmptyCount + 1
As you work with UserForms, you’ll probably think of other uses for the Tag property.