首页 文章

使用Excel 2007 VBA和Cell的NumberFormat属性来设置数字文本的格式

提问于
浏览
0

我想在使用MsgBox和/或连接文本字符串显示相关数字时使用VBA和单元格格式 .

我已经尝试使用单元格的NumberFormat属性,但得到无法使用的结果......

例如以下示例代码应该告诉用户向柠檬水收取4.50美元的费用,标注价格比成本高出150%......

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    MsgBox "I should charge " & Format(Range("a3").Value * 1.5, Range("a3").NumberFormat) & " for this lemonade."
End Sub

...结果是...

Sample output when the source cell is unformatted and formatted as currency...

显示了两种情况:第一种是未格式化的单元格;第二种是货币格式的单元格 . 我希望我的代码能够容纳任何有效的数字格式,包括自定义格式 .

1 回答

  • 1

    只要您使用美国区域设置,就可以使用 Application.Text 而不是 Format

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        MsgBox "I should charge " & Application.Text(Range("a3").Value * 1.5, Range("a3").NumberFormat) & " for this lemonade."
    End Sub
    

    例如 . 如果您没有美国设置,则仍需要解决任何本地化的货币格式 .

相关问题