VBA: Set decimal places based on variable?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I'm looking for a way to set the decimal places for text i'm outputting by counting the decimal places of another variable.

Im thinking I could count the length of the input, and figure out how many decimals it is, then set the format of the output based on that. But I'm not 100% sure how...

Maybe something like:
Code:
intInLen = Len(dblTextIn) <--length of input number
intInDec = InStr(dblTextIn,".") <--location where first decimal occurs
intDec = intInLen - intInDec <--how many decimal places I need
strTextOut= Format(dblTextOut, ".000"[<--zeros based on intDec])<--but how??

I feel like there has got to be a better way than what I'm thinking above...

I just need to set decimal places of strTextOut to be the same as my input.
Ex:
If my input is 50.125:
I want strTxtOut format to be set to ".000"

Or if my input is 50.12
I want the format for strTxtOut to be set to ".00"

Or if my input is 0.5625
I want the format for strTxtOut to be ".0000"

Anyone know how best to do this?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
strTextOut= Format(dblTextOut, ".000"[<--zeros based on intDec])<--but how??
Anyone know how best to do this?

This statement should do what you want...
Code:
strTextOut = Format(dblTextOut, "." & String(intDec, "0"))
 
Last edited:
Upvote 0
Untested code, combining your several lines into one and using REPT

Code:
strTextOut = Format(dblTextOut, "0." & WorksheetFunction.Rept("0", Len(dblTextIn) - InStr(dblTextIn, ".")))
 
Upvote 0
Rept is exactly what I was looking for. Thanks!
Just to point out, we can combine all your lines of code into a single statement using VB's own built-in String function instead of calling out to the worksheet's REPT function (I thought you had used individual statements for use later on in your code)...
Code:
strTextOut = Format(dblTextOut, "." & String(Len(dblTextIn) - InStr(dblTextIn, "."), "0"))
 
Upvote 0
Actually Rick, I didnt even see your original response, but that does the trick and its a native funciton. Very cool. Thanks guys.

I just set the variables for length and "." location because then I can use them elsewhere and even if I dont, its easier for me to keep track of the code and edit it later.
 
Last edited:
Upvote 0
Or you could just see if the integer part of the number is 0:

Code:
Sub test()
Dim x As Double, y As String
x = 0.13475
y = x
If Int(x) = 0 Then y = Mid(y, 2)
MsgBox y
End Sub
 
Upvote 0
If you don't want the initial 0 stripped off, such as in 0.5437, just assign the numeric value to the string variable.
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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