Formatting number in VBA

  • Thread starter Thread starter John Pierce
  • Start date Start date
J

John Pierce

I would like to have the result of the following line display with
only one
decimal place. As written, it consistently displays to five decimal
places. Obviously, because the output location is defined with a
variable, I can't
pre-format the cell it will be in. numQs and numWrong are Integers.
Cells(I + 2, "B").Formula = "=" & (numQs - numWrong) / numQs & "*100"
 
With Cells(I + 2, "B")
.Formula = "=" & Format((numQs - numWrong) / numQs,"0.0") & "*100"

End With


Would be my guess at what you want.

Another interpretation might be:

With Cells(I + 2, "B")
.Formula = "=" & (numQs - numWrong) / numQs,"0.0" & "*100"
.NumberFormat:="#,##0.0"
End With

The first puts one decimal place in the formula, the second formats the cell
to only display one decimal place for the formula result.
 
Back
Top