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
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,235
Office Version
  1. 365
Platform
  1. Windows
Try Format.

Format(variable, "0.0000")
 

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622

ADVERTISEMENT

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.
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
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")
 

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
Ok excellent, finally got it. Thanks for the help!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,517
Messages
5,602,115
Members
414,505
Latest member
quoctrungvu99

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