Use formula text() in VBA (or equivalent)

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I want to store a number as text, formatted to 4 decimal places. Doing this within the spreadsheet, you can do: =text(value, "0.0000")

How can I accomplish the same thing in VBA? I'd rather just have the result and not a result with a formula in the cell.


Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I was using cstr() and it works to convert a number to a string. But I need to format the string number to 4 decimal places.

Example: 45 becomes "45.0000"

If cstr worked like this: cstr(variable, "0.0000") that would be what I'm after.
 
Upvote 0
Try Format.

Format(variable, "0.0000")
 
Upvote 0
I can get it to display "45.0000" but in the formula bar, you can see that the value is still 45.

Any thoughts on getting the .0000 to become part of the value? If I store 45.0000 as text, then it reverts back to just 45.
 
Upvote 0
Hi

If you want to store the value as text you can format the cell as text, ex.:

Code:
    Range("A1").NumberFormat = "@"
    Range("A1").Value = Format(45, "0.0000")
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

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