Ratio in textbox

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,075
Office Version
  1. 2019
Platform
  1. Windows
I have this code that allows the user to just type numbers in the textbox to avoid typing in the wrong format. I would like to know is this could be set up to allow for typing in a ratio. (i.e. 2:1, 10:1, etc.) so the user avoids typing in the wrong format. The number "1" should always be be constant. Can anyone assist. Thanks

Code:
Dim v As String

Select Case Len(tbInv)
    Case 1
        tbInv = Format(tbInv, "$0\.00")
    Case Is > 1
        v = Replace(tbInv, "$", "")
        v = Replace(v, ".", "")
        tbInv = Format(CCur(v) / 100, "$#,#0.00")
    Case Else
End Select
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Here is some code that might do the trick:
Code:
Private DisableChangeEvent As Boolean
Private Sub TextBox1_Change()
    If DisableChangeEvent Then Exit Sub
    If TextBox1.Text = VBA.Constants.vbNullString Then Exit Sub
    Dim Colon As Boolean
    Dim i As Long
    Dim ValidText As String
    Dim Char As String
    DisableChangeEvent = True
    For i = 1 To VBA.Strings.Len(TextBox1.Text) Step 1
        Char = VBA.Strings.Mid(TextBox1.Text, i, 1)
        If Colon Then
            If Char = "1" Then
                ValidText = ValidText + Char
                Exit For
            End If
        Else
            If Char = ":" Then
                ValidText = ValidText + Char
                Colon = True
            ElseIf IsNumeric(Char) Then
                ValidText = ValidText + Char
            End If
        End If
    Next i
    TextBox1.Text = ValidText
    DisableChangeEvent = False
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1.Text = VBA.Constants.vbNullString Then Exit Sub
    If Not VBA.Strings.Right(TextBox1.Text, 2) = ":1" Then
        If VBA.Strings.Right(TextBox1.Text, 1) = ":" Then
            TextBox1.Text = TextBox1.Text & "1"
        Else
            TextBox1.Text = TextBox1.Text & ":1"
        End If
    End If
End Sub
Please remember to back up any work before running new code.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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