several textbox using only with numbers

inactiveUser214710

Board Regular
Joined
Apr 27, 2012
Messages
171
Hello everyone
I have 4 textbox used only for values(numbers-cash).
I use the code below for each textbox.
Is it possible to elaborate a macro, and how, to avoid repeating the codes for each textbox?
Thank you for your cooperation.
Code:
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Only Numbers in textbox or simples signs


   Select Case KeyAscii
      Case Asc("0") To Asc("999")
      Case Asc(".")
        If InStr(1, TextBox4.Value, ".") > 0 Then
          KeyAscii = 0
        End If

        Case Asc(",")
        If InStr(1, TextBox4.Value, ",") > 0 Then
          KeyAscii = 0
        End If

      Case Else
         KeyAscii = MsgBox("SORRY! Introduce just Price numbers")


   End Select

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi,
where you have repeating code that returns a value, then make a function of the code & call it as required.

try this update to your code & see if helps

Code:
Function NumbersOnly(ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger
'Only Numbers in textbox or simples signs
    Select Case KeyAscii
    Case 44, 46, 48 To 57
'valid entries [0-9] "." ","
    Case Else
'cancel
        KeyAscii = 0
    End Select
    Set NumbersOnly = KeyAscii
End Function


To call it from your textboxes

Code:
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = NumbersOnly(KeyAscii)
End Sub

The updated code should only allow numeric values or your specified characters to be entered.

Dave
 
Upvote 0
Hi,
where you have repeating code that returns a value, then make a function of the code & call it as required.

try this update to your code & see if helps

Code:
Function NumbersOnly(ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger
'Only Numbers in textbox or simples signs
    Select Case KeyAscii
    Case 44, 46, 48 To 57
'valid entries [0-9] "." ","
    Case Else
'cancel
        KeyAscii = 0
    End Select
    Set NumbersOnly = KeyAscii
End Function


To call it from your textboxes

Code:
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = NumbersOnly(KeyAscii)
End Sub

The updated code should only allow numeric values or your specified characters to be entered.

Dave

Hi Dave
Perfect. Thank you very much for your help.
solved
Carlos
 
Upvote 0
I added a "Paste event handler" and checked the entire text in TextBox.

See if this works.

Code:
' Paste event
Private Sub TextBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
            ByVal Action As MSForms.fmAction, _
            ByVal Data As MSForms.DataObject, _
            ByVal X As Single, ByVal Y As Single, _
            ByVal Effect As MSForms.ReturnEffect, _
            ByVal Shift As Integer)
    TextBox1 = validateTB(TextBox1)
End Sub
'Change event
Private Sub TextBox1_Change()
    TextBox1 = validateTB(TextBox1)
End Sub

Function validateTB(tb) As String
    Dim i, shold
    Application.EnableEvents = False
    For i = 1 To Len(tb)
        If Mid(tb.Text, i, 1) Like "[0-9]" Then _
                    shold = shold & Mid(tb.Text, i, 1)
    Next i
    validateTB = shold
    Application.EnableEvents = True
End Function
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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