Only stop when left of Decimal Point is changed

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    With TextBox1
        If InStr(.Text, ".") > 0 Then
            If Len(Split(.Text, ".")(1)) = [COLOR=#ff0000]2[/COLOR] Then KeyAscii = 0
        End If
        Select Case KeyAscii
            Case Asc("0") To Asc("9")
            Case Asc("."): If UBound(Split(.Text, ".")) = [B][COLOR=#006400]1[/COLOR][/B] Then KeyAscii = 0
            Case Else: KeyAscii = 0
        End Select
    End With
End Sub

Can it be that after the two decimal places achieved I can still change the whole number part part not the DecimalPart?

That's when I am adding to the left part of the ".", allow me.

But to the right, don't.

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This code will allow you to enter "123.45"
It will allow you to change the last two digits of that number or to enter numbers before the decimal point.

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    With TextBox1
        If .Text Like "*.*" Then
            If .Text Like "*." Or .Text Like "*.#" Then
                GoSub ValidateNumeric
            Else
                If .SelStart < InStr(1, .Text, ".") Then
                    GoSub ValidateNumeric
                Else
                    If .SelLength > 0 Then
                        GoSub ValidateNumeric
                    Else
                        KeyAscii = 0
                    End If
                End If
            End If
        Else
            If KeyAscii <> Asc(".") Then
                GoSub ValidateNumeric
            End If
        End If
    End With
    If KeyAscii = 0 Then Beep
    Exit Sub
ValidateNumeric:
    Select Case KeyAscii
        Case Is < Asc("0")
            KeyAscii = 0
        Case Is > Asc("9")
        KeyAscii = 0
            Case Else
    End Select
    Return
End Sub
 
Upvote 0
This code will allow you to enter "123.45"
It will allow you to change the last two digits of that number or to enter numbers before the decimal point.

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    With TextBox1
        If .Text Like "*.*" Then
            If .Text Like "*." Or .Text Like "*.#" Then
                GoSub ValidateNumeric
            Else
                If .SelStart < InStr(1, .Text, ".") Then
                    GoSub ValidateNumeric
                Else
                    If .SelLength > 0 Then
                        GoSub ValidateNumeric
                    Else
                        KeyAscii = 0
                    End If
                End If
            End If
        Else
            If KeyAscii <> Asc(".") Then
                GoSub ValidateNumeric
            End If
        End If
    End With
    If KeyAscii = 0 Then Beep
    Exit Sub
ValidateNumeric:
    Select Case KeyAscii
        Case Is < Asc("0")
            KeyAscii = 0
        Case Is > Asc("9")
        KeyAscii = 0
            Case Else
    End Select
    Return
End Sub


Great !!!!


More than enough.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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