简体   繁体   English

使用VBA将单元格格式化为任意货币,而不考虑区域设置

[英]Format a cell as arbitrary currency regardless of locale, using VBA

This is really bugging me as it seems pretty illogical the way it's working. 这真的很烦我,因为它的工作方式似乎很不合逻辑。

I have a macro to format a cell as a currency using a bit of code to obtain the currency symbol. 我有一个宏来格式化单元格作为货币使用一些代码来获得货币符号。

Here is the code involved: 这是涉及的代码:

Dim sym As String
sym = reportConstants(ISOcode)
    'Just use the ISO code if there isn't a symbol available
    If sym = "" Then
    sym = ISOcode
    End If
With range(.Offset(0, 3), .Offset(3, 3))
.NumberFormat = sym & "#,##0;(" & sym & "#,##0)"
Debug.Print sym & "#,##0;(" & sym & "#,##0)"
End With

reportConstants is a dictionary object with currency symbols defined as strings. reportConstants是一个字典对象,货币符号定义为字符串。 Eg reportConstants("USD") = "$" . 例如reportConstants("USD") = "$" This is defined earlier in the macro. 这是在宏的早期定义的。

When the macro runs it gets the ISO code and should then format the cell with the corresponding currency symbol. 当宏运行时,它获取ISO代码,然后应使用相应的货币符号格式化单元格。

When I run it in one instance the ISO code is "USD" - so sym is defined as "$" - but it still formats the cell with a pound sign (£). 当我在一个实例中运行它时,ISO代码是“USD” - 因此sym被定义为"$" - 但它仍然用井号(£)格式化单元格。 When I debug.print the format cell string it shows $#,##0;($#,##0) so, as long as I got my syntax correct, it should use a dollar sign in the cell. 当我debug.print格式单元格字符串时,它显示$#,##0;($#,##0)所以,只要我的语法正确,就应该在单元格中使用美元符号。 But it uses a £ sign instead. 但它使用的是£符号。 (I am running a UK version of excel so it may be defaulting to £-sign, but why?) (我正在运行英国版的excel,因此它可能默认为£-sign,但为什么呢?)

Any help greatly appreciated. 任何帮助非常感谢。

I just recorded a macro to set the format to $xx.xx and it created this: [$$-409]#,##0.00 . 我刚刚录制了一个宏来将格式设置为$ xx.xx并创建了这个: [$$-409]#,##0.00 Looks like the -409 localises the currency to a particular country; 看起来-409将货币本地化到特定国家; it works without it - try changing yours to .NumberFormat = "[$" & sym & "]#,##0.00" 它没有它工作 - 尝试将你的更改为.NumberFormat = "[$" & sym & "]#,##0.00"

Btw guess I read your question somewhat after posting ;) Excel is well influenced by the regional settings of your computer for currency, language, dates... Using numberformat can force it to keep the sign you require. Btw猜测我在发布后有点读了你的问题;)Excel受到计算机区域设置的影响,因为货币,语言,日期...使用numberformat可以强制它保留你需要的标志。 if it is a matter of rounding up you can try to: On Excel 2010, go to File - Options - Advanced and scroll down to "When calculating this workbook" and click on the "set precision as displayed" and OK out. 如果是四舍五入的问题,您可以尝试:在Excel 2010上,转到文件 - 选项 - 高级,向下滚动到“计算此工作簿时”,然后单击“显示设置精度”并单击“确定”。

Try this: given your values are numerics/ integers/decimals.... 试试这个:给出你的值是数字/整数/小数....

Range("a2").Style = "Currency"

Or you can use format: 或者你可以使用格式:

Format(value, "Currency")

Format(Range(a2).value, "Currency")

References: 参考文献:

http://www.mrexcel.com/forum/excel-questions/439331-displaying-currency-based-regional-settings.html http://www.mrexcel.com/forum/excel-questions/439331-displaying-currency-based-regional-settings.html

http://www.addictivetips.com/microsoft-office/excel-2010-currency-values/ http://www.addictivetips.com/microsoft-office/excel-2010-currency-values/

(PS: I am on mobile, you may try these two links) (PS:我在手机上,你可以尝试这两个链接)

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

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