Add a specific number of decimals places before a number

ArnMan

Board Regular
Joined
Aug 30, 2017
Messages
69
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,
I was wondering how I could get a number from a cell, Lets say A1 = 3, and take another number, could range from 0 to 10, from D1 = 2, or something and take the value in A1 and turn it in .03 in cell C5, using VBA.
D1 is going to set the number of decimal places including the value in A1.
Examples
A1 3
D1 0
C5 3

A1 3
D1 4
C5 .0003

A1 3
D1 3
C5 .003

A1 3
D1 8
C5 .00000003

I know I could C5.formula and do all kinds of if statements, but I was hoping for more VBA simpleness.

Any help would be greatly appreciated.

Thank you in advance.
 
Re: Would like to add a specific number of decimals places before a number

I wanted to thank you for helping
I was able to use the formula perfectly It works great it gave me the value I was looking for and have been able to apply it to my project.

Thank you again

For the formula...

=IFERROR(0+("."&REPT(0,D$1-1)&A1),"")

For the code...
Code:
[table="width: 500"]
[tr]
	[td]Sub MakeIntegersToFloatingPoint()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF((D1=0)+(@=""""),IF(@="""","""",@),"".""&REPT(0,D1-1)&@)", "@", Addr))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Re: Would like to add a specific number of decimals places before a number

I think this formula would do it:

=A1/(10^D1)
 
Upvote 0
Re: Would like to add a specific number of decimals places before a number

I think this formula would do it:

=A1/(10^D1)
Your formula returns the number in cell A1 when D1 is 0 or blank... the OP seemed to indicate in Message #8 (and seemed to confirm in Message #11 ) that he did not want to display anything when D1 was 0 or blank.
 
Last edited:
Upvote 0
Re: Would like to add a specific number of decimals places before a number

He didn't really say that he wanted nothing, just not the error. However if that is the case:

=IF(D1=0,"",A1/(10^D1))
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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