Text/Number Validation in Text Boxes
Copy the following code in a general module and call it from any of the textboxes' keypress event.
For its usage download the attachment.
Function AllowTextOnly(AscKey As MSForms.ReturnInteger, Optional blnConvUpperCase _ As Boolean = False, Optional blnAllowSpace As Boolean = True) As Integer Select Case AscKey Case Asc("a") To Asc("z"), Asc("A") To Asc("Z"), Asc(" ") If blnConvUpperCase Then AllowTextOnly = Asc(UCase(Chr(AscKey))) 'Convert to uppercase Else AllowTextOnly = AscKey End If Case Else AllowTextOnly = 0 End Select If blnAllowSpace = False And AscKey = Asc(" ") Then AllowTextOnly = 0 End If End Function Function AllowNumOnlyDecimalOptional(strText As String, AscKey As _ MSForms.ReturnInteger, Optional blnAllowDecimal As Boolean = False) As Integer Select Case AscKey Case Asc(0) To Asc(9) AllowNumOnlyDecimalOptional = AscKey Case Else If blnAllowDecimal = True And AscKey = Asc(".") Then AllowNumOnlyDecimalOptional = AscKey Else AllowNumOnlyDecimalOptional = 0 End If End Select If Len(strText) > 0 Then If Not IsNumeric(strText & Chr(AscKey)) Then AllowNumOnlyDecimalOptional = 0 End If End If End Function Function AllowNumOnlyNoDecimal(AscKey As MSForms.ReturnInteger) As Integer Select Case AscKey Case Asc(0) To Asc(9) AllowNumOnlyNoDecimal = AscKey Case Else AllowNumOnlyNoDecimal = 0 End Select End Function
Attachment | Size |
---|---|
TextBoxDataEntryValidation.xls | 43.5 KB |
»
- Vishesh's blog
- Login or register to post comments
- 4947 reads
Recent comments
5 years 36 weeks ago
6 years 22 weeks ago
6 years 34 weeks ago
6 years 37 weeks ago
6 years 38 weeks ago
6 years 43 weeks ago
6 years 52 weeks ago
7 years 2 days ago
7 years 3 days ago
7 years 3 days ago