I have this code:
Sub NewSummary()
Set ws = ThisWorkbook.Sheets("Sheet1")
iWorking = 3
iTotal = 4
dPercent = iWorking / iTotal
pPercent = Format(dPercent, Percent)
sWorking = iWorking & " / " & iTotal
sWorking = Format(sWorking)
ws.Cells(1, 1).Value = sWorking
ws.Cells(2, 1).Value = pPercent
End Sub
The output that I expect is:
3 / 4, 75.00%
But what I get is:
4-Mar, 0.75
Excel is overriding the "3 / 4" string and assuming I want a date. How do I suppress that? And why is the Format function not converting 0.75 to 75.00%?
Percent
declared or assigned in your code? Hint: it's not. So it's an empty variant. You shouldDim
all variables (usingOption Explicit
at the top of modules will help enforce this, and there are IDE settings you can toggle to make this your default). – David Zemens Aug 16 '19 at 14:35Percent
(which as an empty variant evaluates to""
orvbNullString
), which is not"Percent"
:) – David Zemens Aug 16 '19 at 17:06