Setting Text Box criteria in a user form

NoCalRun

New Member
Joined
Sep 15, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello everyone, be gentle as I'm new. I used the following code to only allow text bx entries for keycodes 1 through 9, back space, tab, key number pad 0 through 9 and decimal. All are functional with the exception of the decimal.
Can anyone make a suggestion as to how to get it to work?
Most appreciative.
Private Sub TextBox6_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Select Case KeyCode
Case vbKey0 To vbKey9, vbKeyBack, vbKeyDecimal, vbKeyTab, vbKeyBack, vbKeyNumpad0 To vbKeyNumpad9
Case Else
KeyCode = False
End Select

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You are using the numeric key pad? The other dot character is a period, not a decimal.
 
Upvote 0
You are using the numeric key pad? The other dot character is a period, not a decimal.
I thought about that however I did not see any vbakeys mentioning period.
Is there a work around?
 
Upvote 0
To elaborate, if using the dot/period beside the ? key then add 190 to your list. I couldn't find a vb constant for the period.
BTW, this is basic troubleshooting by stepping through your code and checking the values of variables and references. Mouse over KeyCode when stepping through and you'd see that the key I'm talking about has an integer value of 190.
 
Upvote 0
I used the following code to only allow text bx entries for keycodes 1 through 9, back space, tab, key number pad 0 through 9 and decimal. All are functional with the exception of the decimal.
If the purpose is to allow only number & dot as input, try something like this:
Use textbox_Change event instead of KeyDown.

VBA Code:
Private Sub TextBox6_Change()
Dim tx As String
Static oldVal As String

With TextBox6
    tx = .Text
        If (IsNumeric(tx) And Not tx Like "*[!0-9.]*" And Not tx Like "*.*.*") Or tx = "." Or Len(tx) = 0 Then
            oldVal = .Text
        Else
            .Text = oldVal: Beep
        End If
End With

End Sub

Example:
True : "12", "3.44" , ".2", "."
False: "-10" "2.30E+05", "+2", "2.35.5", "2,000", "23e4", "23a", "$23", "1,2", "1 2"
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

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