Formatting number in VBA

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"
 
T

Tom Ogilvy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top