Consulting

Results 1 to 4 of 4

Thread: Solved: How to get AVERAGE of range into a cell

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    38
    Location

    Solved: How to get AVERAGE of range into a cell

    I need some help on figuring out how to determine the AVERAGE of a range of values, and get that AVERAGE value into a specific cell.

    the variable Month is an INTEGER

    [vba]
    Range("J2").Offset(RowOffset:=1, ColumnOffset:=1).Resize(Month).Select

    [/vba]

    Now what I would like to do is this:
    [vba]
    Cells(3, "C") = Average.Selection
    [/vba]

    I know that isnt the way to do it... but you can get the idea.

    How can I do this in VBA?
    Dave

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

    Range("C3").Value = Application.Average(Range("J2").Offset(1, 1).Resize(Month))
    [/vba]

    Not the best idea to use Month as a variable name, seeing as it is a VBA function.
    ____________________________________________
    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

  3. #3
    VBAX Regular
    Joined
    Jul 2008
    Posts
    38
    Location
    Amazing... you are really great!

    I continue to learn this... and I am kinda wondering how that works... after researching the Application object, searching for AVERAGE... it wasn't there... yet I realise now... that this is really what you are doing.

    [VBA]
    Range("C3").Value = Application.WorksheetFunction.Average(Range("J2").Offset(1, 1).Resize(Month))
    [/VBA]

    How is it that we can leave off the WorksheetFunction part ? Curious.

    THANKS AGAIN!
    Dave

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because the Application is Excel, and Average is part of the Application. IT is also in the WorksheetFunctions collection, but oddly, not all are, and error handling is different with Application and WorksheetFunction.
    ____________________________________________
    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
  •