Give currency format to a cell value in vba

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Give currency format to a cell value in vba

Post by YasserKhalil »

Hello everyone
I have a table and I need to format the fifth column within that table to be currency (dollar) and I used this line

Code: Select all

.ListColumns(5).DataBodyRange.NumberFormat = "$#,##0.00"
It works but formats the column as local currency, not the dollar currency. Any idea how to fix that?

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Give currency format to a cell value in vba

Post by YasserKhalil »

I have searched and found a workaround like that

Code: Select all

.ListColumns(5).DataBodyRange.NumberFormat = "[$$-en-US]#,##0"

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Give currency format to a cell value in vba

Post by HansV »

Alternatively:

Code: Select all

.ListColumns(5).DataBodyRange.NumberFormat = "\$#,##0.00"
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Give currency format to a cell value in vba

Post by YasserKhalil »

Thanks a lot my tutor. so you mean the backslash used before the format does the trick?

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Give currency format to a cell value in vba

Post by HansV »

Some characters in a number format string have a special meaning: for example m d h s $ # 0 . , - ( ) ;
A backslash before such a character tells Excel to treat it as a literal character.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Give currency format to a cell value in vba

Post by YasserKhalil »

Thanks a lot my tutor.
Best Regards