PDA

View Full Version : Format Function with Numbers



achin
04-21-2008, 06:57 AM
I tried to declare the variable as Range and output it in percentage format like below. But could not run accordingly.
Dim LValue As Range

LValue = Format(0.981, "Percent")


How should i change the range format to %?

dominicb
04-21-2008, 07:04 AM
Good afternoon achin

When in doubt about a variable, get Excel to tell you!

MsgBox TypeName(LValue)

You need to declare it as a string.

HTH

DominicB

Norie
04-21-2008, 07:32 AM
achin

What are you actually trying to do?

That code will fail since the Format function returns a string not a range.

Paul_Hossler
04-21-2008, 03:48 PM
As is, LValue is Nothing since you didn't Set it to a cell or cells

Since the default property of the Range object is .Value, you don't really need to use LValue.Value (althougth I like to)


Sub One()
Dim LValue As Range

Set LValue = Range("a1")

LValue = Format(0.981, "Percent")


End Sub


Paul

achin
04-21-2008, 06:04 PM
Hi, Paul!

Do you mean like this?


Sub One()
Dim LValue As Range

Set LValue = Range("a1")

.Value = Format(0.981, "Percent")


End Sub

Paul_Hossler
04-22-2008, 12:47 PM
Close



LValue.Value = Format(0.981, "Percent")



Note that this would be a string that happens to look like a number. You might want to use .NumberFormat



Sub Two()
Dim LValue As Range

Set LValue = Range("a1")

LValue.Value = .981

LValue.NumberFormat = "_(#,##0.0%_);(#,##0.0%);0.0%_)"

End Sub