使用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.
以下代码涉及:
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
是具有定义为字符串的货币符号的字典对象。例如。 reportConstants(USD)=$
。这在宏中的前面定义。
reportConstants
is a dictionary object with currency symbols defined as strings. E.g. reportConstants("USD") = "$"
. This is defined earlier in the macro.
当宏运行时,它获得ISO代码,然后使用相应的货币符号格式化单元格。
When the macro runs it gets the ISO code and should then format the cell with the corresponding currency symbol.
当我在一个实例中运行它时,ISO代码是USD - 所以 sym
被定义为$
- 但它仍然使用英镑(£)格式化单元格。当我 debug.print
格式单元格字符串,它显示 $#,## 0;($#,## 0)
所以,只要我的语法正确,它应该在单元格中使用一个美元符号。但是它使用了一个£符号。 (我正在运行一个英国版的excel,所以它可能是默认为$ sign,但为什么?)
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 (£). 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. But it uses a £ sign instead. (I am running a UK version of excel so it may be defaulting to £-sign, but why?)
任何帮助非常感谢。
推荐答案
我刚刚录制了一个宏,将格式设置为$ xx.xx,并创建了这个: [$$ - 409]# ,## 0.00
。看起来,-409将货币定位到特定国家;它没有它工作 - 尝试将您的更改为 .NumberFormat =[$& sym& ]#,## 0.00
I just recorded a macro to set the format to $xx.xx and it created this: [$$-409]#,##0.00
. Looks like the -409 localises the currency to a particular country; it works without it - try changing yours to .NumberFormat = "[$" & sym & "]#,##0.00"
这篇关于使用VBA将单元格格式化为任意货币,而不管语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!