+ Reply to Thread
Results 1 to 5 of 5

Array of Ranges to work out average value PROBLEM!

Hybrid View

AndresJanes Array of Ranges to work out... 07-19-2014, 08:30 PM
TMS Re: Array of Ranges to work... 07-19-2014, 08:47 PM
mikerickson Re: Array of Ranges to work... 07-19-2014, 08:49 PM
AndresJanes Re: Array of Ranges to work... 07-19-2014, 08:56 PM
mikerickson Re: Array of Ranges to work... 07-19-2014, 09:12 PM
  1. #1
    Registered User
    Join Date
    07-19-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    2

    Angry Array of Ranges to work out average value PROBLEM!

    I'm trying to create an array of ranges to use with the average function, I would like each range to be processed and the average value found to be printed. I'm getting error 1004 "unable to get average property of worksheet function"

    Private Sub CommandButton3_Click()
        Dim Ranges(1 To 7) As String
        Dim i As Long
        Dim RngAvrg1 As Long
          
    Ranges(1) = "C:C"
    Ranges(2) = "D:D"
    Ranges(3) = "E:E"
    Ranges(4) = "F:F"
    Ranges(5) = "G:G"
    Ranges(6) = "H:H"
    Ranges(7) = "I:I"
    
    i = 1
    
    Do Until i = 7
    
    RngAvrg1 = Application.WorksheetFunction.Average(Worksheets("Task_Data").Range(Ranges(i)))
            Sheets("Task_Data").Range("L" & i).Value = RngAvrg1
            
    i = i + 1
    
    Loop
     
        
        
    End Sub
    Last edited by AndresJanes; 07-19-2014 at 09:02 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,167

    Re: Array of Ranges to work out average value PROBLEM!

    Maybe:

    Private Sub CommandButton3_Click()
        Dim Ranges(1 To 7) As String
        Dim i As Long
        Dim RngAvrg1 As Long
          
    Ranges(1) = "C:C"
    Ranges(2) = "D:D"
    Ranges(3) = "E:E"
    Ranges(4) = "F:F"
    Ranges(5) = "G:G"
    Ranges(6) = "H:H"
    Ranges(7) = "I:I"
    
    For i = 1 To 7
    
           RngAvrg1 = Application.WorksheetFunction.Average(Worksheets("Task_Data").Range(Columns(i)))
           Sheets("Task_Data").Range("L" & i).Value = RngAvrg1
    
    Next 
     
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Array of Ranges to work out average value PROBLEM!

    From my testing, if there are no numeric cells in any of those columns AVERAGE will return the #DIV/0 error if in a cell.
    In your use, with VBA, this results in the "Unable to get Average property" error message.
    Application.Worksheet.Average will result in a run time error.
    Application.Average will return an error value (but will then crash because RngAvrg1 is data type Long which is a type mismatch with the data type Error that it will return)

    If you declare RngAvrg1 as Variant and use Application.Average rather than Application.Worksheet.Average that should fix things.
    Private Sub CommandButton3_Click()
        Dim Ranges(1 To 7) As String
        Dim i As Long
        Dim RngAvrg1 As Variant: ' <<<<<<<<<<
          
        Ranges(1) = "C:C"
        Ranges(2) = "D:D"
        Ranges(3) = "E:E"
        Ranges(4) = "F:F"
        Ranges(5) = "G:G"
        Ranges(6) = "H:H"
        Ranges(7) = "I:I"
        
        i = 1
        
        Do Until i = 7
            RngAvrg1 = Application.Average(Worksheets("Task_Data").Range(Ranges(i))) ' <<<<<<<
            Sheets("Task_Data").Range("L" & i).Value = RngAvrg1
            i = i + 1
        Loop
     
    End Sub
    or you could replace the whole thing with
    With Sheets("Task_Data").Range("L1:L7")
        .FormulaR1C1 = "=AVERAGE(INDEX(C3:C9, 0, ROW()))"
        '.value = .value
    End With
    |
    Last edited by mikerickson; 07-19-2014 at 08:54 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    07-19-2014
    Location
    England
    MS-Off Ver
    2013
    Posts
    2

    Re: Array of Ranges to work out average value PROBLEM!

    I tried your code

    With Sheets("Task_Data").Range("L1:L7")
        .FormulaR1C1 = "=AVERAGE(INDEX(C3:C9, 0, ROW()))"
        '.value = .value
    End With
    It does the job. Can you explain how this works? for future reference

    **** EDIT

    Both solutions solved the problem. Thank you.
    Last edited by AndresJanes; 07-19-2014 at 09:05 PM.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Array of Ranges to work out average value PROBLEM!

    It puts a formula in each cell. The cute part isn't the use of VBA, but using INDEX with the row argument set to 0 to return a whole column of the range of the array.

    INDEX(anArray, 0, x) returns the x'th column of anArray
    INDEX(anArray, y, 0 ) returns the y'th row of anArray.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to work out the average of an array from a changing cell
    By francesc in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-06-2014, 10:00 PM
  2. Array problem with Average
    By bronsonb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-28-2011, 02:09 PM
  3. Average & Min Function Problem in Array Formula
    By mubashir aziz in forum Excel General
    Replies: 8
    Last Post: 05-20-2009, 02:07 AM
  4. select variables ranges, copy to array, paste the array in new workbook
    By Mathew in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-01-2005, 05:06 AM
  5. Array Problem - Ranges
    By TonyRowland in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-24-2005, 05:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1