Results 1 to 7 of 7

Thread: RESOLVED[Variable format]

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Resolved RESOLVED[Variable format]

    While defining a variable as Long, how can I format it in order to obtain the number separated with comma?
    Last edited by Fonty; Jun 3rd, 2006 at 10:39 PM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Variable format

    Use the FormatCurrency or FormatNumber function:
    VB Code:
    1. MsgBox FormatCurrency(num)
    2. MsgBox FormatNumber(num)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Variable format

    very strange.. I posted an answer.. its nowhere to be found!???

    hmmmmmm
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Variable format

    For example, I have
    VB Code:
    1. Dim var As Long
    How do I specify that I try to apply the format to this single variable?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Variable format

    VB Code:
    1. var = Format(var, "###,#0")

  6. #6

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Variable format

    This works perfectly for
    VB Code:
    1. Dim var As Long
    2. var = Format(var, "###,#0")
    3. Range("A1").Value = var
    However it fails for
    VB Code:
    1. Dim var As Long
    2. var = Format(var, "###,#0")
    3. Range("A1").Value = "(" & - var ", " & var & ")"
    Is it possible to make a union with a formated variable?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Variable format

    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:
    VB Code:
    1. Dim var As Long
    2. Range("A1").Value = "(-" & Format(var, "###,#0") ", " & Format(var, "###,#0") & ")"

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width