ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Formatting number in VBA (https://www.excelbanter.com/excel-programming/283258-formatting-number-vba.html)

John Pierce

Formatting number in VBA
 
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"

Tom Ogilvy

Formatting number in VBA
 
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.


--
Regards,
Tom Ogilvy




"John Pierce" wrote in message
om...
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"




Mike Tomasura

Formatting number in VBA
 
try something like this


Selection.NumberFormat = "0.0"



"John Pierce" wrote in message
om...
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"





All times are GMT +1. The time now is 05:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com