PDA

Click to See Complete Forum and Search --> : RESOLVED[Variable format]


Fonty
Jun 2nd, 2006, 12:44 PM
While defining a variable as Long, how can I format it in order to obtain the number separated with comma?

Pradeep1210
Jun 2nd, 2006, 12:50 PM
Use the FormatCurrency or FormatNumber function:
MsgBox FormatCurrency(num)
MsgBox FormatNumber(num)

Static
Jun 2nd, 2006, 12:53 PM
very strange.. I posted an answer.. its nowhere to be found!???

hmmmmmm

Fonty
Jun 2nd, 2006, 01:39 PM
For example, I have
Dim var As Long
How do I specify that I try to apply the format to this single variable?

Hack
Jun 2nd, 2006, 01:55 PM
var = Format(var, "###,#0")

Fonty
Jun 2nd, 2006, 03:15 PM
This works perfectly for
Dim var As Long
var = Format(var, "###,#0")
Range("A1").Value = var
However it fails for
Dim var As Long
var = Format(var, "###,#0")
Range("A1").Value = "(" & - var ", " & var & ")"
Is it possible to make a union with a formated variable?

si_the_geek
Jun 3rd, 2006, 05:22 AM
As var is declared as a Long, it cannot contain the formatting (only the numbers). You need to use the format function when you are converting the value to a string, eg:
Dim var As Long
Range("A1").Value = "(-" & Format(var, "###,#0") ", " & Format(var, "###,#0") & ")"

Your first example should be:
Dim var As Long
Range("A1").Value = Format(var, "###,#0")