VBA to format cells...

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
Hey all

Does anyone know the syntax / if its possible to explicitly state what the format of the data in a cell should take when I am assigning a value to it?

An example should make it clearer

Code:
Cells(5,3) = 120

Now if the cell C5 is formatted as currency, it would be displayed as £120.00. Similarly, if it was formatted as a percentage it would display 12000%. What I want to do is state how I want it formatted as I am writing to the cell. So, what i'm after is something like this:

Code:
Cells(5,3) = Currency(120)

I need to either display values as plain text, currency (to 2 d.p) or as a percentage.


Can anyone help?!?!


Thanks, Patrick
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Macropheliac

Board Regular
Joined
Aug 26, 2005
Messages
165
Try this:
For Currency:
Code:
Cells(5,3).NumberFormat = "$#,##0.00"
For Text:
Code:
Cells(5,3)..NumberFormat = "@"
For Percentage:
Code:
Cells(5,3)..NumberFormat = "0.00%"
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
I need to either display values as plain text, currency (to 2 d.p) or as a percentage.
Well, to simply assign a value & format to those specific formats:
Currency:
With Range("C5")
.Value = 120
.NumberFormat = "$#,##0.00"
End With

Percentage:
With Range("C5")
.Value = 120
.NumberFormat = "0.00%"
End With

Text:
With Range("C5")
.Value = 120
.NumberFormat = "@"
End With


Now, if you want it to be formatted as one or the other of these, depending on what the value
is, what would be your criteria for each of the formats?
 

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
Cheers both of you, excellent stuff.

My criteria depends on the 'mode' that the user is viewing the sheet in, but that's not really my problem. I just needed to know the VBA to vary output as opposed to selecting the range and applying the formatting all at once.

Which is better, in your opinion, stating the format as they are plugged in, or plugging them in, then selecting the whole range and changing the format?
 

Forum statistics

Threads
1,137,367
Messages
5,681,079
Members
419,950
Latest member
BeckiJae

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