Consulting

Results 1 to 9 of 9

Thread: Solved: Basic VBA problem with Format

  1. #1

    Solved: Basic VBA problem with Format

    Hello all,

    I'm new here, really just getting some experience with VBA with excel, although I have some experience with other similar languages I've used, I'm having a little trouble with this one.

    I've searched the net and haven't been able to find quite what I'm looking for.

    I am trying to build a simple function that can output the percent change and format as a percente in excel.



    [VBA]Function PercentChange(CurrentValue, PreviousValue)
    PercentChange = ((CurrentValue - PreviousValue) / PreviousValue)
    FormatPercent (PercentChange,1)
    End Function[/VBA]


    However, there is an error on the format command when I am running it, user error obviously, except I do not understand what I am doing wrong.

    Sorry this is a super basic question, but I'm just getting my feet wet!

    -Mark

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    you didnt declare any of your variables or assign values to them? this might be the problem

  3. #3
    Hmm, I suppose that could be a problem, although if I omit the FormatPercent line from the code, the function works in excel, it just doesn't automatically format it as a percent.

  4. #4
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    [VBA]
    Function PercentChange(CurrentValue, PreviousValue)
    PercentChange = FormatPercent((CurrentValue - PreviousValue) / PreviousValue)
    End Function

    [/VBA]

  5. #5
    Well initially I was getting an error but now that seems to be working. Thanks.

  6. #6
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    no problemo, FormatPercent automatically does two decimal places i dont know if having 1 place was particularly important to you but i think you would have to NumberFormat first:

    [VBA]
    PercentChange = FormatPercent(Format((CurrentValue - PreviousValue) / PreviousValue), "#,##0.0")[/VBA]

    or something to that effect

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Q: does FormatPercent return a string or a double. I'd guess string.

    Would that be a problem trying to calculate with it?

    Paul

  8. #8
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    Seems to me it does return a string...might be a deal breaker?
    ------------------------------------------------
    Happy Coding my friends

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by CatDaddy
    no problemo, FormatPercent automatically does two decimal places i dont know if having 1 place was particularly important to you but i think you would have to NumberFormat first:

    [VBA]
    PercentChange = FormatPercent(Format((CurrentValue - PreviousValue) / PreviousValue), "#,##0.0")[/VBA]

    or something to that effect
    FormatPercent has a NumDigitsAfterDecimal argument.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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