clsTextboxMask is a powerful VBA class that allows creating text fields with input masks in Excel and other Office applications. It provides input validation, placeholder display, and visual indication of field fill status.
- Support for various input mask types (numbers, dates, time, text, regular expressions)
- Real-time input validation
- Display of placeholders with different statuses (empty, partially filled, completely filled, incorrect)
- Visual indication of input correctness through border color
- Support for numeric values with range, sign, and decimal constraints
- Support for variable-length text
- Support for validation via regular expressions
- Support for placeholder color customization based on field status
- Support for placeholder templates with markers: {mask}, {filled}, {remaining}, {holder}, {RegexPattern}, {RegexFilter}, {percent}
- Copy the
clsTextboxMask.clsfile to your VBA project - Use the class in your UserForms
| Property | Type | Description |
|---|---|---|
TextBox |
MSForms.TextBox | Reference to the text box to which the mask is applied |
LabelPlaceholder |
MSForms.Label | Reference to the placeholder label that displays hints |
Mask |
String | Input mask defining allowed characters |
Value |
String | Current value of the text field |
CurrentMaskType |
enumTypeMask | Type of current mask |
Min |
Single | Minimum value for numeric fields |
Max |
Single | Maximum value for numeric fields |
IsNegative |
Boolean | Whether negative values are allowed |
IsDecemal |
Boolean | Whether decimal values are allowed |
BorderColorValid |
Long | Border color when input is correct |
BorderColorInvalid |
Long | Border color when input is incorrect |
PlaceholderEmptyColor |
Long | Placeholder text color for empty field |
PlaceholderPartialColor |
Long | Placeholder text color for partially filled field |
PlaceholderCompleteColor |
Long | Placeholder text color for completely filled field |
PlaceholderInvalidColor |
Long | Placeholder text color for field with incorrect data |
PlaceholderEmpty |
String | Placeholder text for empty field |
PlaceholderPartial |
String | Placeholder text for partially filled field |
PlaceholderComplete |
String | Placeholder text for completely filled field |
PlaceholderInvalid |
String | Placeholder text for field with incorrect data |
The class supports the following mask types:
| Mask Type | Value | Description |
|---|---|---|
tOtherFix |
1 | Fixed mask with various characters |
tDateFix |
2 | Fixed mask for dates |
tTimeFix |
3 | Fixed mask for time |
tNumeric |
4 | Numeric mask with range limitation capability |
tVariableLen |
5 | Variable-length mask |
tRegex |
6 | Regular expression-based mask |
Adds a numeric field with specified validation parameters.
Dim numField As New clsTextboxMask
Call numField.AddFieldNumeric(inputTextBox:=Me.TextBox1, _
minValue:=0, _
maxValue:=100, _
allowDecimal:=True, _
allowNegative:=False)Parameters:
inputTextBox- text field to which the mask is appliedminValue- minimum allowed valuemaxValue- maximum allowed valueallowDecimal- permission for decimal values inputallowNegative- permission for negative values inputshowPlaceholder- placeholder display (optional)numberFormat- number display format (optional)BorderColorValid- border color when input is correct (optional)BorderColorInvalid- border color when input is incorrect (optional)PlaceholderColor- placeholder color (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalid- placeholder text for field with incorrect data (optional)PlaceHolderTemplete- placeholder template (optional)
Adds a date input field with specified validation parameters.
Dim dateField As New clsTextboxMask
Call dateField.AddFieldDate(inputTextBox:=Me.TextBox2, _
dateMask:="##.##.####", _
minDate:=#1/1/2020#, _
maxDate:=#12/31/2030#, _
dateFormat:="dd.mm.yyyy")Parameters:
inputTextBox- text field to which the mask is applieddateMask- date input maskminDate- minimum allowed datemaxDate- maximum allowed datedateFormat- date display format (optional)showPlaceholder- placeholder display (optional)BorderColorValid- border color when input is correct (optional)BorderColorInvalid- border color when input is incorrect (optional)PlaceholderColor- placeholder color (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalid- placeholder text for field with incorrect data (optional)PlaceHolderTemplete- placeholder template (optional)
Adds a time input field with specified validation parameters.
Dim timeField As New clsTextboxMask
Call timeField.AddFieldTime(inputTextBox:=Me.TextBox3, _
timeMask:="##:##", _
minTime:=#0:00:00#, _
maxTime:=#23:59#, _
timeFormat:="hh:mm")Parameters:
inputTextBox- text field to which the mask is appliedtimeMask- time input maskminTime- minimum allowed timemaxTime- maximum allowed timetimeFormat- time display format (optional)showPlaceholder- placeholder display (optional)BorderColorValid- border color when input is correct (optional)BorderColorInvalid- border color when input is incorrect (optional)PlaceholderColor- placeholder color (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalid- placeholder text for field with incorrect data (optional)PlaceHolderTemplete- placeholder template (optional)
Adds a text field with a specified input mask.
Dim textField As New clsTextboxMask
Call textField.AddFieldText(inputTextBox:=Me.TextBox4, _
textMask:="+7(*##) @# A# #Б#") ' Letters-digitsParameters:
inputTextBox- text field to which the mask is appliedtextMask- text input maskshowPlaceholder- placeholder display (optional)BorderColorValid- border color when input is correct (optional)BorderColorInvalid- border color when input is incorrect (optional)PlaceholderColor- placeholder color (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalid- placeholder text for field with incorrect data (optional)PlaceHolderTemplete- placeholder template (optional)
Adds a field with variable text length.
Dim varField As New clsTextboxMask
Call varField.AddFieldVariableLength(inputTextBox:=Me.TextBox5, _
maxLength:=10, _
textMask:="##")Parameters:
inputTextBox- text field to which the mask is appliedmaxLength- maximum text lengthtextMask- text input mask (optional)showPlaceholder- placeholder display (optional)BorderColorValid- border color when input is correct (optional)BorderColorInvalid- border color when input is incorrect (optional)PlaceholderColor- placeholder color (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalid- placeholder text for field with incorrect data (optional)PlaceHolderTemplete- placeholder template (optional)
Adds a field with validation via regular expression.
Dim regexField As New clsTextboxMask
Call regexField.AddFieldRegex(inputTextBox:=Me.TextBox6, _
RegexPattern:="^[A-Z]{2}\d{4}$", _
RegexFilter:="[A-Z0-9]")Parameters:
inputTextBox- text field to which the mask is appliedRegexPattern- regular expression pattern for validationRegexFilter- regular expression filtershowPlaceholder- placeholder display (optional)BorderColorValid- border color when input is correct (optional)BorderColorInvalid- border color when input is incorrect (optional)PlaceholderColor- placeholder color (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalid- placeholder text for field with incorrect data (optional)PlaceHolderTemplete- placeholder template (optional)
Checks the correctness of data entered in the text field.
If myField.IsValid() Then
MsgBox "Data is correct!"
Else
MsgBox "Data is incorrect!"
End IfClears the text field.
myField.Clear()Sets focus to the text field.
myField.SetFocus()Removes the text box mask item and associated components.
myField.RemoveItem()When creating text masks, the following symbols are used:
| Symbol | Description |
|---|---|
# |
Digits (0-9) |
@ |
Latin letters (A-Z, a-z) |
A |
Latin letters and digits (A-Z, a-z, 0-9) |
Б |
Cyrillic letters |
б |
Cyrillic letters and digits |
* |
Any characters |
Dim numField As New clsTextboxMask
Call numField.AddFieldNumeric(inputTextBox:=Me.TextBox1, _
minValue:=0, _
maxValue:=100, _
allowDecimal:=True, _
allowNegative:=False)Dim dateField As New clsTextboxMask
Call dateField.AddFieldDate(inputTextBox:=Me.TextBox2, _
dateMask:="##.##.####", _
minDate:=#1/2020#, _
maxDate:=#12/31/2030#, _
dateFormat:="dd.mm.yyyy")Dim textField As New clsTextboxMask
Call textField.AddFieldText(inputTextBox:=Me.TextBox3, _
textMask:="+7(*##) @# A# #Б#") ' Letters-digits- MSForms.TextBox
- MSForms.Label
- VBScript.RegExp (for regular expression validation)
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
