Consulting

Results 1 to 8 of 8

Thread: Solved: CAGR formula using only range as input

  1. #1

    Solved: CAGR formula using only range as input

    Can someone please check this formula to see why I keep getting a #VALUE error when I use it in the sheet? Thanks!

    [VBA]
    Function CAGR(rng As Range) As Double
    Dim firstCell As Range
    Dim lastCell As Range
    Dim firstVal As Double
    Dim lastVal As Double

    firstCell = rng.Cells(1, 1)
    lastCell = rng.Cells(1, rng.Count)
    firstVal = Range(firstCell).Value
    lastVal = Range(lastCell).Value

    Debug.Print firstCell, lastCell, firstVal, lastVal

    CAGR = (lastVal / firstVal) ^ (1 / rng.Count) - 1

    End Function[/VBA]


    Also, does anyone know how to use Debug.Print to see what the values of the variables are for a function (like this) that is used as a formula in the sheet? I've never had the need to see variable values in this case (and it didn't work!).

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    put a breakpoint in the program before the line "CAGR = ..." then when you hover over the variables with the mouse it will tell you their values
    ------------------------------------------------
    Happy Coding my friends

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I think that since firstCell and lastCell are already Ranges, you don't want to use Range(firstCell)

    Probably something like this

    [VBA]
    firstCell = rng.Cells(1, 1)
    lastCell = rng.Cells(1, rng.Count)
    firstVal = firstCell.Value
    lastVal = lastCell.Value

    [/VBA]


    Paul

  4. #4
    @CatDaddy, I inserted a breakpoint, but none of the variables have values. Since firstVal and lastVal depend on firstCell and lastCell parsing the range info correctly, there must be something wrong with my use of ".Cells(x,y)" in the following part:

    [VBA]
    firstCell = rng.Cells(1, 1)
    lastCell = rng.Cells(1, rng.Count)[/VBA]

    Any thoughts?


    @Paul, good point, but that still didn't resolve the error. Any other ideas?

    Thanks!

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Any other ideas?
    Might be the way you're calling it then

    Can you post a small WB showing the problem?

    Paul

  6. #6
    Here you go.
    Attached Files Attached Files

  7. #7
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    4
    Location
    Try it now

    [VBA]Function CAGR(rng As Range) As Double
    Dim firstval As Double
    Dim lastval As Double
    firstval = rng.Cells(1, 1).Value
    lastval = rng.Cells(1, rng.Columns.Count).Value
    CAGR = (lastval / firstval) ^ (1 / rng.Columns.Count) - 1
    End Function[/VBA]
    Attached Files Attached Files

  8. #8
    Thanks! Based on your solution, I'm not sure why mine didn't work (especially since firstCell and lastCell are ranges). But, even so, your code is more efficient anyway. Take care!

Posting Permissions

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