简体   繁体   English

如何使用Excel VBA按照货币标准格式化列中的数字?

[英]How to Format numbers in a column as per currency standards using excel vba?

I want to format all cells of a column. 我想格式化一列的所有单元格。 The data is a number and I want to add a decimal from left keeping only two digits after that. 数据是一个数字,我想在左侧添加一个小数,之后仅保留两位数字。 Then format it as per currency standards. 然后按照货币标准对其进行格式化。 For eg: 例如:

Data:       Output:
10000       100.00
112233      1,122.33
123456789   1,234,567.89

The requirement is normal and output is in italics. 要求是正常的,输出以斜体显示。

下面将给定的列格式化为货币:

Sheets("Sheet1").Columns("A").NumberFormat = "£#,##0.00"

Macro recorder is a bit dangerous in this case, because if you use it, your code would look like this (from the other answer): 在这种情况下,宏记录器会有些危险,因为如果使用它,您的代码将看起来像这样(来自其他答案):

Sheets("Sheet1").Columns("A").NumberFormat = "£#,##0.00"

However, this is only ok in UK. 但是,这仅在英国可以。 If you want to write code, which formats currency based on the local settings of the PC, then it is a good idea to use the currency there: 如果要编写代码,该代码根据PC的本地设置来格式化货币,那么最好在此处使用货币:

Sheets("Sheet1").Columns("A").Style = "Currency"

Then a UK person would get pounds format and a US one dollars. 然后,一个英国人将得到英镑格式和一美元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM