Using VBA Macro to format a column into Comma Style

kahlua17

New Member
Joined
Feb 15, 2007
Messages
22
I have a macro that creates an email based on the contents of each column in the worksheet. The macro works great, but I would like to format a column (A) which contains numbers into the Comma Style, prior to sending out the email.

Is there a VBA code that will format a given column (A) into a certain style (Currency, Comma, Percentage, etc) ?

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Some examples...

Code:
Columns("A:A").NumberFormat = "General"
Columns("A:A").NumberFormat = "Comma"
Columns("A:A").NumberFormat = "Percent"
Columns("A:A").NumberFormat = "Currency"
 
Upvote 0
Thanks. I also found that this works.

Code:
Columns("A:A").Select
Selection.NumberFormat = "#,##0.00"
 
Upvote 0
Thanks. I also found that this works.

Code:

Columns("A:A").Select
Selection.NumberFormat = "#,##0.00"

True, but selecting a column/row/range to apply formatting (or to do just about anything!) is inefficient, as it will slow down your code.
 
Upvote 0
True. However, it turns out that it doesn't work for what I need. I have a formula that runs through several rows and collects information from each row, generates an email, and sends that out. The way the numbers are loaded into the file, there is no formatting. So $1,000.50 shows up as 1000.5 which is wrong. The formatting of the cell only changes its appearance, and what I need to do is change the actual number. The code that I have which captures the cell that contains the number is :

Code:
& cell.Offset(0, -2).Value

Thanks.
 
Upvote 0
Instead of Value use Text.
Code:
& cell.Offset(0, -2).Text
 
Upvote 0
Eh, you indicated it was formatted?:o

Text should return the value as it's formatted. ie how you see it
 
Upvote 0
It's funny. The first time I tried the .Text extension, it didn't work. Now it does work, but it leaves a large leading space, even though there are no characters preceding the number.

Here is the output (I manually entered in the _ to indicate the blank spaces:

Report Number 123456 ($ __________1,000.50 ).

Here is the full code of that line :

Code:
"Report Number " & cell.Offset(0, -4).Value & " ($ " & cell.Offset(0, -2).Text & ").

I find that resizing the column does have an effect as well. If I resize it so that no values are showing, I get a # sign. If I resize it so that it fits all of the values, I still get a large preceding space.
 
Upvote 0

Forum statistics

Threads
1,223,364
Messages
6,171,646
Members
452,415
Latest member
mansoorali

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