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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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%"
 
Upvote 0
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?
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

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