Consulting

Results 1 to 6 of 6

Thread: Format Function with Numbers

  1. #1
    VBAX Regular achin's Avatar
    Joined
    Apr 2008
    Posts
    13
    Location

    Format Function with Numbers

    I tried to declare the variable as Range and output it in percentage format like below. But could not run accordingly.
    [VBA]Dim LValue As Range

    LValue = Format(0.981, "Percent")
    [/VBA]

    How should i change the range format to %?

  2. #2
    VBAX Regular
    Joined
    Jan 2006
    Posts
    28
    Location

    Smile

    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

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    achin

    What are you actually trying to do?

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

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    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)

    [vba]
    Sub One()
    Dim LValue As Range

    Set LValue = Range("a1")

    LValue = Format(0.981, "Percent")


    End Sub
    [/vba]

    Paul

  5. #5
    VBAX Regular achin's Avatar
    Joined
    Apr 2008
    Posts
    13
    Location
    Hi, Paul!

    Do you mean like this?
    [VBA]
    Sub One()
    Dim LValue As Range

    Set LValue = Range("a1")

    .Value = Format(0.981, "Percent")


    End Sub
    [/VBA]

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Close


    [vba]
    LValue.Value = Format(0.981, "Percent")
    [/vba]


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

    [VBA]
    Sub Two()
    Dim LValue As Range

    Set LValue = Range("a1")

    LValue.Value = .981

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

    End Sub
    [/VBA]

Posting Permissions

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