【问题标题】:Revert a dollars to pounds conversion将美元转换为英镑
【发布时间】:2022-06-28 15:21:39
【问题描述】:

我的美国公司在英国有一个部门,我们偶尔想看看以英镑和美元为单位的提案的外观。

此代码将会计格式的特定范围从美元切换到英镑。

For Each cel In lookRng
    If cel.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" Then
        cel.NumberFormat = "_-[$£-en-GB]* #,##0.00_-;-[$£-en-GB]* #,##0.00_-;_-[$£-en-GB]* ""-""??_-;_-@_-"
    End If
Next cel

我尝试使用相同的方法将英镑兑换成美元。

For Each cel In lookRng
    If cel.NumberFormat = "_-[$£-en-GB]* #,##0.00_-;-[$£-en-GB]* #,##0.00_-;_-[$£-en-GB]* ""-""??_-;_-@_-" Then
        cel.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
    End If
Next cel

屏幕闪烁,格式没有变化。

【问题讨论】:

  • Debug.Print cel.NumberFormat的即时窗口中的结果是什么?

标签: excel vba number-formatting


【解决方案1】:

Excel 正在更改 NumberFormat,将 [$£-en-GB] 更改为 [$£-809]。

请注意,新的 NumberFormat 包含双引号。将格式转换为字符串的最简单方法是使用Replace()将双引号加倍。

?Replace(ActiveCell.NumberFormat, Chr(34), String(2, 34))
_-[$£-809]* #,##0.00_-;-[$£-809]* #,##0.00_-;_-[$£-809]* ""-""??_-;_-@_-
Public Const UKNumberFormat  As String = "_-[$£-809]* #,##0.00_-;-[$£-809]* #,##0.00_-;_-[$£-809]* ""-""??_-;_-@_-"
Public Const USNumberFormat  As String = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"


Sub SwitchNumberFormats(lookRng)
    Dim Cell As Range

    For Each Cell In lookRng
        If Cell.NumberFormat = UKNumberFormat Then
            Cell.NumberFormat = USNumberFormat
        ElseIf Cell.NumberFormat = USNumberFormat Then
            Cell.NumberFormat = UKNumberFormat
        End If
    Next

End Sub

【讨论】:

  • 谢谢!这完美地修复了它
  • @VBABegineer 没问题。你可能想看看我的编辑。
  • @VBABegineer 我打错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多