Format a TextBox on a Userform to 2 decimal places

Zaurb

New Member
Joined
Mar 6, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hi!

Am having a similar issue as described in this post.

My code is as follows:

VBA Code:
Private Sub cmdCalculate_Click()
If IsNumeric(Me.txt_odds.value) Then
    Me.txt_odds = Format(txt_odds.value, "0" & Application.DecimalSeparator & "00")
End If
Me.txt_Minimum = Format(100 / txt_odds.value, "0" & Application.DecimalSeparator & "00%")
End Sub

However, I get strange results. For example, if I input 1.5 in the first textbox and expect to see it as 1.50 after clicking calculate button, I see instead 15.00. However, if i write 2 it display correctly as 2,00.
Can you please help me?

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

EXCEL MAX

Active Member
Joined
Nov 11, 2020
Messages
392
Office Version
  1. 2016
Platform
  1. Windows
Hello Zaurub,
try to do something like this...
VBA Code:
Private Sub cmdCalculate_Click()
    
    Dim vTR As String
    
    vTR = Replace(Me.txt_odds.Value, ".", ",")
    If IsNumeric(Me.txt_odds.Value) Then
         Me.txt_Minimum = Format(100 / vTR, "0.00") & "%"
    End If
   
End Sub
 

Zaurb

New Member
Joined
Mar 6, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hello Zaurub,
try to do something like this...
VBA Code:
Private Sub cmdCalculate_Click()
   
    Dim vTR As String
   
    vTR = Replace(Me.txt_odds.Value, ".", ",")
    If IsNumeric(Me.txt_odds.Value) Then
         Me.txt_Minimum = Format(100 / vTR, "0.00") & "%"
    End If
  
End Sub
Thank you very much!
Results calculating correctly. But i still don't get 2 decimal points. Is there any fix for that? Thanks again!
 

Zaurb

New Member
Joined
Mar 6, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Thank you very much!
Results calculating correctly. But i still don't get 2 decimal points. Is there any fix for that? Thanks again!
2 decimal points in txt_odds textbox
 

Zaurb

New Member
Joined
Mar 6, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
resolved making a little modification as follows:
VBA Code:
Dim vTR As String

    vTR = Replace(Me.txt_odds.value, ".", ",")
    Me.txt_odds = Format(vTR, "0.00")
    If IsNumeric(Me.txt_odds.value) Then
         Me.txt_Minimum = Format(100 / vTR, "0.00") & "%"
    End If
 

EXCEL MAX

Active Member
Joined
Nov 11, 2020
Messages
392
Office Version
  1. 2016
Platform
  1. Windows
Yes, now is correct. I'm glad that you were capable to understand the point.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,795
Messages
5,638,382
Members
417,025
Latest member
MusterDuster

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
Top