Formatting A Number In VBA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have the number 0 in cell O3 of my workksheet. I am trying to place that number into another cell in another workbook using the following VBA line of code. I wish for the number to take on the format of ##### in this cell. So the result for O3 = 0 would be "00000" in the destination cell. This line of code isn't formatting the value, it's displaying as simply 0.

Code:
.Range("J" & drow) = Format(ws_form.Range("O3"), "00000")
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi Ark68,

if the cell is formatted as General you just get a 0. What about

VBA Code:
With .Range("J" & drow)
  .Value = Range("O3").Value
  .NumberFormat = "00000"
End With
as that would choose the proper format for the target cell.

Ciao,
Holger
 
Upvote 0
Hi Ark68,

the above code will display the value in the cell like 00000 while in the formula bar you would see only 0.

You can convert the number to text by using

VBA Code:
.Range("J" & drow) = "'" & Format(ws_form.Range("O3"), "00000")

This would display 00000 with a green triangle in the cell and '00000 in the formula bar with ' referencing that it is a text.

Ciao,
Holger
 
Upvote 0
Solution
Great stuff Holger, thank you! I opted to use the second option. No calculations will
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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