Textboxes for numbers and others for letters in vba

inactiveUser214710

Board Regular
Joined
Apr 27, 2012
Messages
171
Hi everybody
I have a userform in vba with 3 text boxes to put only numbers and signs, and 2 text boxes to fill with numbers or letters (descriptions).
How can I separate them, as to their respective functions, using perhaps a general code in a module. Is this possible?
In my case I use these codes for each number text box.
for example:
VBA Code:
Private Sub txtvt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)                  'txtv1;txtv2;txtvt
   Select Case KeyAscii
      Case Asc("0") To Asc("999")
      Case Asc(".")
             If InStr(1, txtvt.Value, ".") > 0 Then
                KeyAscii = 0
             End If  
      Case Asc(",")
            If InStr(1, txtvt.Value, ",") > 0 Then
            KeyAscii = 0
           End If    
      Case Else
            KeyAscii = MsgBox("SORRY! Introduce just Price numbers")
   End Select
End Sub

I would like to simplify that in a module or in a class module, for textbox of numbers, and another for textbox with different writing (numbers and letters). will that be possible or am I exaggerating with my pretensions?

In my userform I have (txtv1; txtv2; txtvT) to enter numbers and signs (monetary values) and (txtOb1; txtOb2) to enter letters and numbers.
Thank you in advance, for anyone who can help me in this problem.
Thanks again.
Jdcar
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
One option for you to consider

Create a single function that can be used for both types of text boxes and call the function like this

Rich (BB code):
Private Sub txtOb1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = Validate(0, KeyAscii)
End Sub
(do this for txtOb1, txtOb2)

Rich (BB code):
Private Sub txtv1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = Validate(1, KeyAscii)
End Sub
(do this for txtv1, txtv2, txtvT)

Place function in userform code window
Tailor the function to allow additional characters if I have missed any that are allowed
VBA Code:
Private Function Validate(txt As Integer, ByVal Ascii As Integer) As Integer
'letters and numbers
    If txt = 0 Then
        Select Case Ascii
            Case 48 To 57, 65 To 90, 97 To 122
                Validate = Ascii
        End Select
'monetary values
    ElseIf txt = 1 Then
        Select Case Ascii
            Case 43, 45, 48 To 57
                Validate = Ascii
        End Select
    End If
End Function
 
Upvote 0
One option for you to consider

Create a single function that can be used for both types of text boxes and call the function like this

Rich (BB code):
Private Sub txtOb1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = Validate(0, KeyAscii)
End Sub
(do this for txtOb1, txtOb2)

Rich (BB code):
Private Sub txtv1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = Validate(1, KeyAscii)
End Sub
(do this for txtv1, txtv2, txtvT)

Place function in userform code window
Tailor the function to allow additional characters if I have missed any that are allowed
VBA Code:
Private Function Validate(txt As Integer, ByVal Ascii As Integer) As Integer
'letters and numbers
    If txt = 0 Then
        Select Case Ascii
            Case 48 To 57, 65 To 90, 97 To 122
                Validate = Ascii
        End Select
'monetary values
    ElseIf txt = 1 Then
        Select Case Ascii
            Case 43, 45, 48 To 57
                Validate = Ascii
        End Select
    End If
End Function
 
Upvote 0
Hi yongle
The option presented works very well. Thank you.
With a quikly answer as well as the availability spent, I was very grateful.
Best regards
jdcar
 
Upvote 0
Hi Yongle
In the past thread worked but later I found that it didn't allow commas, points or spaces.
how to enter this data to make it work?
VBA Code:
Private Function Validate(txt As Integer, ByVal Ascii As Integer) As Integer
I apreciated your help
[B]'letters and numbers[/B]


    If txt = 0 Then

        Select Case Ascii

            Case 48 To 57, 65 To 90, 97 To 122

                Validate = Ascii

        End Select

[B]'monetary values      Here I would like put extensive to ( “.” ) – points ; ( “,” ) – commas e ( “ “ ) spaces[/B]

    ElseIf txt = 1 Then

        Select Case Ascii

            Case 43, 45, 48 To 57

                Validate = Ascii

        End Select

    End If

End Function



Private Sub txtOb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

KeyAscii = Validate(0, KeyAscii)

End Sub

Private Sub txtvt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

KeyAscii = Validate(1, KeyAscii)

End Sub
 
Upvote 0
Please post some typical values to help explain request in post#5
1. values permitted
2. values NOT permitted
 
Upvote 0
Hi Yongle
I managed to overtake with the addition of the code below.
My sincere thanks for the concern of help.
thank you very much.
jdcar
VBA Code:
Private Function Validate(txt As Integer, ByVal Ascii As Integer) As Integer
'letters and numbers
    If txt = 0 Then
        Select Case Ascii
            Case 48 To 57, 65 To 90, 97 To 122
                Validate = Ascii
                Case 8 To 46            '<===I added this code
                Validate = Ascii        '<===I added this code
        End Select
'monetary values
    ElseIf txt = 1 Then
        Select Case Ascii
            Case 43, 45, 48 To 57
                Validate = Ascii
                Case 44 To 46            '<===I added this code
                Validate = Ascii         '<===I added this code
        End Select
    End If
End Function
 
Upvote 0
How about ...

VBA Code:
Private Function Validate(txt As Integer, ByVal Ascii As Integer) As Integer
'letters and numbers
    If txt = 0 Then
        Select Case Ascii
            Case 8 To 46, 48 To 57, 65 To 90, 97 To 122
                Validate = Ascii
        End Select
'monetary values
    ElseIf txt = 1 Then
        Select Case Ascii
            Case 43 To 46, 48 To 57
                Validate = Ascii
        End Select
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top