Autoreplace comma with dot VBA

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
I have this block of code that suppose to change texbox value for example, from 1,5 into 1.5 but for some reason instead of getting 1.5 i get this: 45047,0
VBA Code:
Private Sub HoursCount_AfterUpdate()
    Dim inputText As String
    inputText = Me.HoursCount.Text
    inputText = Replace(inputText, ",", ".")
    ' Format the number with one decimal place
    inputText = Format(inputText, "0.0")
    ' Update the text box with the formatted number
    Me.HoursCount.Text = inputText
End Sub
I thought it is fairly straight forward code, but apparently it's not.
Maybe someone can advise how to fix this or do it differently?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I also tried using this:
VBA Code:
Private Sub HoursCount__Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim inputNumber As String
    Dim formattedNumber As String
   
    ' Get the text that was just entered into the Textbox
    inputNumber = Me.HoursCount.Value
   
    ' Check if the input is a valid number
    If IsNumeric(inputNumber) Then
        ' If it is, format number into 0.0
        formattedNumber = Format(CStr(inputNumber), "0.0")
        ' Update the Textbox with the formatted date
        Me.HoursCount.Value = formattedNumber
    Else
        MsgBox "Error"
        Cancel = True
    End If
End Sub

But that didn't work either
 
Upvote 0
Here's the solution I came up with for future viewers:
The previous code was working but the way excel understood was always 1,5 due to regional decimal separator.

You had to force comma replacement with dot.

VBA Code:
Private Sub HoursCount_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim inputNumber As Double
    Dim formattedNumber As String
   
' Get the text that was just entered into the Textbox
inputNumber = CDbl(Me.HoursCount.Value)

' Check if the input is a valid number
If IsNumeric(inputNumber) Then
    ' If it is, format the number to 1 decimal place
    formattedNumber = Format(inputNumber, "0.0")
    formattedNumber = Replace(inputNumber, ",", ".")
    ' Update the Textbox with the formatted number
    Me.HoursCount.Value = formattedNumber
Else
    MsgBox "Error: Input must be a number."
    Cancel = True
End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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