- Class Overview
- Class Architecture
- Properties
- Methods
- Events
- Constants and Enumerations
- Implementation Details
- Dependencies
The clsTextboxMask class is a powerful VBA tool that allows creating textboxes 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, invalid)
- Visual indication of input correctness through border color
- Support for numeric values with range, sign and decimal restrictions
- 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
Defines the types of supported masks:
tOtherFix(1) - Fixed mask with various characterstDateFix(2) - Fixed mask for datestTimeFix(3) - Fixed mask for timetNumeric(4) - Numeric mask with range limitation capabilitytVariableLen(5) - Variable length masktRegex(6) - Regular expression-based mask
| Property | Type | Description |
|---|---|---|
TextBox |
MSForms.TextBox | Reference to the textbox field to which the mask is applied |
LabelPlaceholder |
MSForms.Label | Reference to the placeholder label that displays hints |
Mask |
String | Input mask that defines allowed characters |
Value |
String | Current value of the textbox field |
CurrentMaskType |
enumTypeMask | Type of the current mask |
Min |
Single | Minimum value for numeric fields |
Max |
Single | Maximum value for numeric fields |
IsDecimal |
Boolean | Are decimal values 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 |
TextBox- Gets or sets the textbox field to which the mask is appliedLabelPlaceholder- Gets or sets the placeholder label that displays hintsMask- Gets or sets the input mask that defines allowed charactersValue- Gets or sets the current value of the textbox fieldCurrentMaskType- Gets or sets the type of the current maskMin- Gets or sets the minimum value for numeric fieldsMax- Gets or sets the maximum value for numeric fieldsIsDecimal- Gets or sets whether decimal values are allowedBorderColorValid- Gets or sets the border color when input is correctBorderColorInvalid- Gets or sets the border color when input is incorrectPlaceholderEmptyColor- Gets or sets the placeholder text color for empty fieldPlaceholderPartialColor- Gets or sets the placeholder text color for partially filled fieldPlaceholderCompleteColor- Gets or sets the placeholder text color for completely filled fieldPlaceholderInvalidColor- Gets or sets the placeholder text color for field with incorrect dataPlaceholderEmpty- Gets or sets the placeholder text for empty fieldPlaceholderPartial- Gets or sets the placeholder text for partially filled fieldPlaceholderComplete- Gets or sets the placeholder text for completely filled fieldPlaceholderInvalid- Gets or sets the placeholder text for field with incorrect data
PlaceholderMask- Gets the current placeholder mask showing remaining characters to be filledPlaceHolderTemplate- Gets or sets the placeholder template with markersVisibleLabelPlaceholder- Gets or sets the visibility of the placeholder labelItems- Gets the collection of all textbox mask itemsCount- Gets the number of items in the collectionRemainingChars- Gets the number of remaining characters to fillFormatValue- Gets or sets the format for displaying the valueLenValue- Gets the length of the current valuelenMask- Gets the length of the maskVersion- Gets version information about the class
Adds a numeric field with specified validation parameters.
Syntax:
Public Sub AddFieldNumeric(ByRef inputTextBox As MSForms.TextBox, _
ByVal minValue As Single, _
ByVal maxValue As Single, _
ByVal allowDecimal As Boolean, _
Optional showPlaceholder As Boolean = True, _
Optional numberFormat As String = "#.0", _
Optional BorderColorValid As XlRgbColor = 0, _
Optional BorderColorInvalid As XlRgbColor = 0, _
Optional PlaceholderEmptyColor As XlRgbColor = 0, _
Optional PlaceholderEmpty As String = vbNullString, _
Optional PlaceholderPartialColor As XlRgbColor = 0, _
Optional PlaceholderPartial As String = vbNullString, _
Optional PlaceholderCompleteColor As XlRgbColor = 0, _
Optional PlaceholderComplete As String = vbNullString, _
Optional PlaceholderInvalidColor As XlRgbColor = 0, _
Optional PlaceholderInvalid As String = vbNullString, _
Optional PlaceHolderTemplate As String = "{holder}")Parameters:
inputTextBox- textbox field to which the mask is appliedminValue- minimum allowed valuemaxValue- maximum allowed valueallowDecimal- allow input of decimal valuesshowPlaceholder- show placeholder (optional)numberFormat- number display format (optional)BorderColorValid- border color for correct input (optional)BorderColorInvalid- border color for incorrect input (optional)PlaceholderEmptyColor- placeholder color for empty field (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartialColor- placeholder color for partially filled field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderCompleteColor- placeholder color for completely filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalidColor- placeholder color for field with invalid data (optional)PlaceholderInvalid- placeholder text for field with invalid data (optional)PlaceHolderTemplate- placeholder template (optional)
Adds a date input field with specified validation parameters.
Syntax:
Public Sub AddFieldDate(ByRef inputTextBox As MSForms.TextBox, ByVal dateMask As String, _
ByVal minDate As Date, _
ByVal maxDate As Date, _
Optional dateFormat As String = "dd.mm.yyyy", _
Optional showPlaceholder As Boolean = True, _
Optional BorderColorValid As XlRgbColor = 0, _
Optional BorderColorInvalid As XlRgbColor = 0, _
Optional PlaceholderEmptyColor As XlRgbColor = 0, _
Optional PlaceholderEmpty As String = vbNullString, _
Optional PlaceholderPartialColor As XlRgbColor = 0, _
Optional PlaceholderPartial As String = vbNullString, _
Optional PlaceholderCompleteColor As XlRgbColor = 0, _
Optional PlaceholderComplete As String = vbNullString, _
Optional PlaceholderInvalidColor As XlRgbColor = 0, _
Optional PlaceholderInvalid As String = vbNullString, _
Optional PlaceHolderTemplate As String = "{holder}")Parameters:
inputTextBox- textbox field to which the mask is applieddateMask- date input maskminDate- minimum allowed datemaxDate- maximum allowed datedateFormat- date display format (optional)showPlaceholder- show placeholder (optional)BorderColorValid- border color for correct input (optional)BorderColorInvalid- border color for incorrect input (optional)PlaceholderEmptyColor- placeholder color for empty field (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartialColor- placeholder color for partially filled field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderCompleteColor- placeholder color for completely filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalidColor- placeholder color for field with invalid data (optional)PlaceholderInvalid- placeholder text for field with invalid data (optional)PlaceHolderTemplate- placeholder template (optional)
Adds a time input field with specified validation parameters.
Syntax:
Public Sub AddFieldTime(ByRef inputTextBox As MSForms.TextBox, ByVal timeMask As String, _
ByVal minTime As Date, _
ByVal maxTime As Date, _
Optional timeFormat As String = "hh:mm", _
Optional showPlaceholder As Boolean = True, _
Optional BorderColorValid As XlRgbColor = 0, _
Optional BorderColorInvalid As XlRgbColor = 0, _
Optional PlaceholderEmptyColor As XlRgbColor = 0, _
Optional PlaceholderEmpty As String = vbNullString, _
Optional PlaceholderPartialColor As XlRgbColor = 0, _
Optional PlaceholderPartial As String = vbNullString, _
Optional PlaceholderCompleteColor As XlRgbColor = 0, _
Optional PlaceholderComplete As String = vbNullString, _
Optional PlaceholderInvalidColor As XlRgbColor = 0, _
Optional PlaceholderInvalid As String = vbNullString, _
Optional PlaceHolderTemplate As String = "{holder}")Parameters:
inputTextBox- textbox field to which the mask is appliedtimeMask- time input maskminTime- minimum allowed timemaxTime- maximum allowed timetimeFormat- time display format (optional)showPlaceholder- show placeholder (optional)BorderColorValid- border color for correct input (optional)BorderColorInvalid- border color for incorrect input (optional)PlaceholderEmptyColor- placeholder color for empty field (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartialColor- placeholder color for partially filled field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderCompleteColor- placeholder color for completely filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalidColor- placeholder color for field with invalid data (optional)PlaceholderInvalid- placeholder text for field with invalid data (optional)PlaceHolderTemplate- placeholder template (optional)
Adds a text field with specified input mask.
Syntax:
Public Sub AddFieldText(ByRef inputTextBox As MSForms.TextBox, _
ByVal textMask As String, _
Optional showPlaceholder As Boolean = True, _
Optional BorderColorValid As XlRgbColor = 0, _
Optional BorderColorInvalid As XlRgbColor = 0, _
Optional PlaceholderEmptyColor As XlRgbColor = 0, _
Optional PlaceholderEmpty As String = vbNullString, _
Optional PlaceholderPartialColor As XlRgbColor = 0, _
Optional PlaceholderPartial As String = vbNullString, _
Optional PlaceholderCompleteColor As XlRgbColor = 0, _
Optional PlaceholderComplete As String = vbNullString, _
Optional PlaceholderInvalidColor As XlRgbColor = 0, _
Optional PlaceholderInvalid As String = vbNullString, _
Optional PlaceHolderTemplate As String = "{holder}")Parameters:
inputTextBox- textbox field to which the mask is appliedtextMask- text input maskshowPlaceholder- show placeholder (optional)BorderColorValid- border color for correct input (optional)BorderColorInvalid- border color for incorrect input (optional)PlaceholderEmptyColor- placeholder color for empty field (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartialColor- placeholder color for partially filled field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderCompleteColor- placeholder color for completely filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalidColor- placeholder color for field with invalid data (optional)PlaceholderInvalid- placeholder text for field with invalid data (optional)PlaceHolderTemplate- placeholder template (optional)
Adds a field with variable text length.
Syntax:
Public Sub AddFieldVariableLength(ByRef inputTextBox As MSForms.TextBox, _
ByVal maxLength As Integer, _
Optional textMask As String = vbNullString, _
Optional showPlaceholder As Boolean = True, _
Optional BorderColorValid As XlRgbColor = 0, _
Optional BorderColorInvalid As XlRgbColor = 0, _
Optional PlaceholderEmptyColor As XlRgbColor = 0, _
Optional PlaceholderEmpty As String = vbNullString, _
Optional PlaceholderPartialColor As XlRgbColor = 0, _
Optional PlaceholderPartial As String = vbNullString, _
Optional PlaceholderCompleteColor As XlRgbColor = 0, _
Optional PlaceholderComplete As String = vbNullString, _
Optional PlaceholderInvalidColor As XlRgbColor = 0, _
Optional PlaceholderInvalid As String = vbNullString, _
Optional PlaceHolderTemplate As String = "{holder}")Parameters:
inputTextBox- textbox field to which the mask is appliedmaxLength- maximum text lengthtextMask- text input mask (optional)showPlaceholder- show placeholder (optional)BorderColorValid- border color for correct input (optional)BorderColorInvalid- border color for incorrect input (optional)PlaceholderEmptyColor- placeholder color for empty field (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartialColor- placeholder color for partially filled field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderCompleteColor- placeholder color for completely filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalidColor- placeholder color for field with invalid data (optional)PlaceholderInvalid- placeholder text for field with invalid data (optional)PlaceHolderTemplate- placeholder template (optional)
Adds a field with validation via regular expression.
Syntax:
Public Sub AddFieldRegex(ByRef inputTextBox As MSForms.TextBox, _
ByVal RegexPattern As String, _
ByVal RegexFilter As String, _
Optional showPlaceholder As Boolean = True, _
Optional BorderColorValid As XlRgbColor = 0, _
Optional BorderColorInvalid As XlRgbColor = 0, _
Optional PlaceholderEmptyColor As XlRgbColor = 0, _
Optional PlaceholderEmpty As String = vbNullString, _
Optional PlaceholderPartialColor As XlRgbColor = 0, _
Optional PlaceholderPartial As String = vbNullString, _
Optional PlaceholderCompleteColor As XlRgbColor = 0, _
Optional PlaceholderComplete As String = vbNullString, _
Optional PlaceholderInvalidColor As XlRgbColor = 0, _
Optional PlaceholderInvalid As String = vbNullString, _
Optional PlaceHolderTemplate As String = "{holder}")Parameters:
inputTextBox- textbox field to which the mask is appliedRegexPattern- regular expression pattern for validationRegexFilter- regular expression filtershowPlaceholder- show placeholder (optional)BorderColorValid- border color for correct input (optional)BorderColorInvalid- border color for incorrect input (optional)PlaceholderEmptyColor- placeholder color for empty field (optional)PlaceholderEmpty- placeholder text for empty field (optional)PlaceholderPartialColor- placeholder color for partially filled field (optional)PlaceholderPartial- placeholder text for partially filled field (optional)PlaceholderCompleteColor- placeholder color for completely filled field (optional)PlaceholderComplete- placeholder text for completely filled field (optional)PlaceholderInvalidColor- placeholder color for field with invalid data (optional)PlaceholderInvalid- placeholder text for field with invalid data (optional)PlaceHolderTemplate- placeholder template (optional)
Checks the correctness of the entered data in the textbox field.
Syntax:
Public Function IsValid() As BooleanReturn Value:
Boolean- True if data is correct, otherwise False
Clears the textbox field.
Syntax:
Public Sub Clear()Sets focus on the textbox field.
Syntax:
Public Sub SetFocus()Removes the textbox mask element and related components.
Syntax:
Public Sub RemoveItem()Event raised when the textbox value changes.
Event raised when a key is pressed in the textbox.
Public Enum enumTypeMask
tOtherFix = 1
tDateFix
tTimeFix
tNumeric
tVariableLen
tRegex
[_First] = tOtherFix
[_Last] = tRegex
End EnumWhen 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 |
The class supports the use of markers in placeholder templates:
{mask}- displays the mask{filled}- number of filled characters{remaining}- number of remaining characters{holder}- placeholder with mask{RegexPattern}- regular expression pattern{RegexFilter}- regular expression filter{percent}- fill percentage
The class automatically handles textbox events:
Change- updates placeholder and checks validityKeyPress- controls input characters according to the mask
- MSForms.TextBox
- MSForms.Label
- VBScript.RegExp (for regular expression validation)