我尝试以编程方式更新 Excel 中的单元格,但由于某种原因,VBA 代码无法以编程方式更新 $,但可以更新“£”、“€”、“¥”。
我可以手动使用 -"$"* #,##0.00-;-"$"* #,##0.00_-;-"$"* "-"??-;_-@自定义格式,但是当使用 VBA 添加它时,我得到了 £。
有问题的单元格没有条件格式,也没有自定义格式,因为我将它们全部清除了。
如果可行的话,您可能建议使用条件格式来执行此操作,不幸的是,我有数百个单元格和几个 CCY 要做,宁愿做得更聪明,也不愿更难。
Sub UpdateCurrencyFormatting()
Dim currencySymbol As String
Dim currencyFormat As String
Dim ws As Worksheet
' Define the specific worksheet
Set ws = ThisWorkbook.Sheets("Step 2. Group Effort")
' Get the currency symbol from the helper cell on the specified worksheet
currencySymbol = ws.Range("J1").Value
' Create the custom number format string using the currency symbol
currencyFormat = "_-""" & currencySymbol & """* #,##0.00_-;-" & _
"""" & currencySymbol & """* #,##0.00_-;_-" & _
"""" & currencySymbol & """* ""-""??_-;_-@"
Debug.Print "Currency Symbol: " & currencySymbol
Debug.Print "Currency Format: " & currencyFormat
' Apply the custom number format to the desired range on the specified worksheet
ws.Range("I6:I7").NumberFormat = currencyFormat
ws.Range("I9:I11").NumberFormat = currencyFormat
End Sub
输出如下:
货币符号:£ 货币格式: -"£"* #,##0.00-;-"£"* #,##0.00_-;-"£"* "-"??-;-@ 货币符号: € 货币格式: -"€"* #,##0.00-;-"€"* #,##0.00-;-"€"* "-"??-;-@ 货币符号:$ 货币格式: -"$"* #,##0.00-;-"$"* #,##0.00-;-"$"* "-"??-;-@ 货币符号: ¥ 货币格式:-"¥"* #,##0.00-;-"¥"* #,##0.00-;-"¥"* "-"??-;_-@
唯一不起作用的是$
Cell J1 是一个辅助单元,我在其上使用以下代码:
=CHOOSE(MATCH('Step 3. Pricing Review'!$L$39, {"JPY","EUR","USD","GBP"}, 0), "¥", "€", "$","£")
我有一个触发代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("J1")) Is Nothing Then
Call UpdateCurrencyFormatting
End If
End Sub
因此,每当有人更改定价审核'!$L$39 CCY 时,它都会自动更新步骤 2. 小组工作'!$J$1 并且上面的代码会触发自动货币格式更新。
如前所述,它适用于除 $ 之外的所有内容,我尝试了各种技术,包括 ASCII:
currencyFormat = "_-""" & Chr(36) & """* #,##0.00_-;-""" & Chr(36) & """* #,##0.00_-;_-""" & Chr(36) & """* ""-""??_-;_-@"
给出以下结果:
货币符号:$ 货币格式: -"$"* #,##0.00-;-"$"* #,##0.00_-;-"$"* "-"??-;_-@
所以现在我需要联系并了解是否有人可以解决问题并且可以提供帮助。
提前致谢
$
符号是VBA中本国货币的通用符号。如果您在 NumberFormat
代码中使用此符号,程序将替换本国货币的符号(根据操作系统设置)。NumberFormatLocal
属性而不是 NumberFormat
。仅在这里,您需要像在您的语言环境中一样应用小数点和千位分隔符。