+ Reply to Thread
Results 1 to 9 of 9

Troubleshoot: averaging a dynamic range of cells across a row

  1. #1
    Registered User
    Join Date
    01-14-2014
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    45

    Troubleshoot: averaging a dynamic range of cells across a row

    Hi all,

    I got stuck with a simple problem but I don't know where I gone wrong, I have been haggling with it in the past hour so any help would be greatly appreciated!

    Optional background information:
    I have a row of dates going across in row 2, and random values going across in row 3 from D3 onwards. I want to return the average of values in row 3 until the third last non-blank cell in row 2 and do a similar thing for maximum row but just for the last 30 cells from the right (so latest 30 days). I have attached screenshot of part of spreadsheet for better illustration.
    Capture.PNG

    In my macro I can come up with a range for both, as below. Both lines of macro below select the correct range I am looking for.
    Please Login or Register  to view this content.
    However when I try to apply them into an average/maximum function they returned an error 1004, Application defined or object define error:
    Please Login or Register  to view this content.
    When I put it as following it returned me the value of Cells (3, 4) (Cell D3) instead of the average:
    Please Login or Register  to view this content.
    Can anyone tell me what did I do wrong please?

    Thanks a lot in advance!
    Attached Images Attached Images
    Last edited by chococ; 02-17-2014 at 02:42 PM.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    To write into a formula, try:
    Please Login or Register  to view this content.
    In your attempts, you are mixing VBA commands and Excel formulae incorrectly.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    01-14-2014
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    Thanks a lot for your quick reply! They worked like a charm. Only one thing, the values turned out locked (eg. =AVERAGE($D$3:... etc) after I ran the macro. Is there a way to make the cell referencing dynamic (like RC3 etc) in this fashion so that I can do a loop on it please...?

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    Yes - use FormulaR1C1, and rewrite your range statements to express the necessary range in R1C1 notation.

  5. #5
    Registered User
    Join Date
    01-14-2014
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    Ah so that's what the FormulaR1C1 is for!! Is it possible to mix the two references though...? I need something like this:
    =AVERAGE(RC[4]: R[0]C[third last to end of row 1])

    So essentially something like:
    =AVERAGE(RC[4], Cells(R[0], Cells(33, Columns.Count).End(xlToLeft).Column - 2))

    um... not sure if it's making sense...

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    So, something like:
    Please Login or Register  to view this content.
    Returns a relative column reference for the first range argument of the average range...

    You have to make sure you separate the stuff which only works within a formula, and wrap it in "quotes", and the stuff which only makes sense in VBA, and leave it outside the quotes.

    Make sense?

  7. #7
    Registered User
    Join Date
    01-14-2014
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    YES YES YES I GOT IT WORKING!!! Yay finally, after a whole day of trawling through the forums... Thank you SO MUCH you saved my day, now I can get off work soon and go home... I have added a reputation for you

  8. #8
    Registered User
    Join Date
    01-14-2014
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    One quick question though: what does the "&" do before and after the Cells (#,#) reference?

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Troubleshoot: averaging a dynamic range of cells across a row

    Glad you figured it out. The '&' simply joins up expressions into one string.

+ 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. Averaging a range of cells if before a certain date
    By markl41 in forum Excel General
    Replies: 9
    Last Post: 07-19-2011, 11:34 AM
  2. averaging variable range of cells
    By wiganda in forum Excel General
    Replies: 0
    Last Post: 09-22-2010, 05:05 PM
  3. averaging variable range of cells
    By wiganda in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2010, 05:02 PM
  4. VBA code for averaging a range of cells
    By kayard in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2006, 06:24 AM
  5. Replies: 0
    Last Post: 08-29-2005, 07:45 AM

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