【发布时间】:2019-01-25 15:48:20
【问题描述】:
我正在学习 VBA,当 VBA 与添加字符串交互时,我注意到一个奇怪的怪癖。当您采用两种版本的代码(使用此处交换的注释部分更改注释部分)时,一个输出带有美元符号的字符串,一个则没有。有人知道这是一个错误还是计划升级?
Option Explicit
Sub CalcCost()
Dim curSalesPrice As Currency
Dim curTotalCost As Currency
Dim sngSalesTax As Single
Dim strMessage As String
curSalesPrice = 35
sngSalesTax = 0.085
Range("A1:B8").ClearContents
Range("A1").Value = "The cost of the calculator"
Range("A4").Value = "Price"
Range("B4").Value = curSalesPrice
Range("A5").Value = "SalesTax"
Range("A6").Value = "Cost"
Range("B5").Value = curSalesPrice * sngSalesTax
'curTotalCost = curSalesPrice + (curSalesPrice * sngSalesTax)
curTotalCost = Format(curSalesPrice + (curSalesPrice * sngSalesTax), "Currency") 'swap here
'strMessage = "The calculator total is " & Format(curTotalCost, "Currency")
strMessage = "The calculator total is " & curTotalCost 'swap here
Range("A8").Value = strMessage
Range("B6").Value = curTotalCost
End Sub
【问题讨论】:
-
在一种情况下,您正在使用 Format 方法将其格式化为货币,而在另一种情况下则没有。 :docs.microsoft.com/en-us/office/vba/language/reference/…
-
为什么会是一个错误?你期望发生什么?