Consulting

Results 1 to 12 of 12

Thread: Solved: averaging variable sized ranges

  1. #1

    Solved: averaging variable sized ranges

    back to wind data !!

    if i want to average a series of wind speeds, then i can write something like this:

    ActiveCell.FormulaR1C1 = "=AVERAGE(R[-1]C[-2]:R[31833]C[-2])

    which averages the numbers in whichever column i happen to select relative to the activecell.

    however, the ranges i'm dealing with change length. some arrays have 31833 rows in (as above) however others have twice that.

    i've been learning something in the last week as i can now set a variable which can hold the number of rows that is in an usedrange. my promlem is that i'm having trouble writing the correct code to do the above average but using a variable instead of a number.


    this is what i have tried, but none work

    ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C[-19]:R[CStr (i)]C[-19])


    where i is the variable.

    could anyone kindly suggest the correct way of writing this in VBA

    mark

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You can use:
    [vba]ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C[-19]:R[" & i & "]C[-19])"
    [/vba]

    PS I assume i is in fact declared as a Long and not an Integer?
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    thank you rory.

    i'm still having problems with writing it in VBA without the dreaded yellow highlighter coming out and stopping the procedure!

    i'm trying to assign the variable fiftyA to equal the average on a different sheet.

    this is my code, which is oh so wrong!

    fiftyA = AVERAGE(sheet1!R[-1]C[-3]:R[" & m & "]C[-3])

    where m is a variable as LONG and Sheet1 is weher all the data is.

    any ideas?

    mark

  4. #4
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You can't just put worksheet formulas into code like that. You can use:
    [VBA]fiftyA = Application.Average(range)[/VBA]
    but it's not clear to me from your example what actual range you want to average - is it relative to the actual cell or is it a specific location?
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Quote Originally Posted by rory
    You can't just put worksheet formulas into code like that. You
    damn it, so that's why it wasn't working too well!

    so it looks like i need to set a range from sheet1, then average that range using the above code.

    i'll try that, thanks

    mark

  6. #6
    okay, so i'm still going round in circles!!

    this is the bits of my code so far which relates to what i'm trying to do



    Range("C2:C" & m).Name = "fifty"'this sets the name of the range

    fiftyA = Application.Average("fifty") 'this is adapted from you above

    Range("b2").Select
    ActiveCell.FormulaR1C1 = fiftyA 'this is the output cell


    not sure what i'm doing wrong. if you have any other ideas why i can't work this out (other than i'm a VBA debutant!!)

    thank you for your help

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Range("C2:C" & m).Name = "fifty" 'this sets the name of the range

    fiftyA = Application.Average(Range("fifty")) 'this is adapted from you above

    Range("b2").Value = fiftyA 'this is the output cell
    [/vba]
    ____________________________________________
    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

  8. #8
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    If you want it entered as a formula, you can use:
    [VBA]Range("B2").FormulaR1C1 = "=AVERAGE(sheet1!R[-1]C[-3]:R[" & m & "]C[-3])"
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    Quote Originally Posted by xld
    [vba]

    fiftyA = Application.Average(Range("fifty")) 'this is adapted from you above

    [/vba]
    i can't believe i was one set of brackets out !! vba can be very frustrating sometimes.

    thank you for helping me find the solutions to my problems and taking your time to do so.


  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You also missed Range!


    __________________________________________
    UK Cambridge XL Users Conference 29-30 Nov
    http://www.exceluserconference.com/UKEUC.html
    ____________________________________________
    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

  11. #11
    Quote Originally Posted by xld
    You also missed Range!
    opps, so i did !! and for a moment there i had my vba confidence up...!

    thanks for noticing that i'm worse than i thought!

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That's quite alright it's my job.
    ____________________________________________
    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
  •