+ Reply to Thread
Results 1 to 10 of 10

Sum to last column in loop

  1. #1
    Registered User
    Join Date
    08-20-2014
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    95

    Sum to last column in loop

    Having a hard time figuring this out.

    Here's what I have so far:

    Please Login or Register  to view this content.
    The sum function will always start at D2, but will continue until the last column (eg until P2). I wanted to make it a more complex function but I couldn't figure out how to get it to reach the last column on the same row.

    I tried to use

    Please Login or Register  to view this content.
    But that made an absolute reference at cell D2 (so the last cell in the "Utilization" column had =sum($D$2:P52) which is not what I'm looking for.

    Thoughts?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Sum to last column in loop

    Hello efarkouh,

    Are you trying to convert the formula into a VBA macro?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    08-20-2014
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    95

    Re: Sum to last column in loop

    Quote Originally Posted by Leith Ross View Post
    Hello efarkouh,

    Are you trying to convert the formula into a VBA macro?
    Yes, I'd like it to be part of my macro.

    In the last column of each of sheets 2-4, I would like there to be a column at the end of the sheet in which I can have the sum of D2 until the last column (eg D2:P2) (the number of columns varies by sheet) divided by the count of columns (except for the last one where the formula is).

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Sum to last column in loop

    Maybe:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-20-2014
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    95

    Re: Sum to last column in loop

    Quote Originally Posted by JOHN H. DAVIS View Post
    Maybe:

    Please Login or Register  to view this content.
    Thanks for the reply, John.

    I tried this:
    Please Login or Register  to view this content.
    What happened was that it used an absolute reference for everything and only for the range of one of the sheets; it looked like this:

    =sum($D$2:$P$2)

    for every cell in the column "Utilization" in all the sheets.

  6. #6
    Registered User
    Join Date
    08-20-2014
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    95

    Re: Sum to last column in loop

    Changing

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    fixes the issue where it wasn't summing a dynamic number of columns, but the references are still absolute.

    Utilization column:

    Sheet 2: =SUM($D$2:$V$2)
    Sheet 3: =SUM($D$2:$I$2)
    Sheet 4: =SUM($D$2:$P$2)

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Sum to last column in loop

    I think you'd need this instead:

    Please Login or Register  to view this content.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Sum to last column in loop

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Sum to last column in loop

    If the references are an issue add the following too your code:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    08-20-2014
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    95

    Re: Sum to last column in loop

    Norie,

    Thanks for that. That got rid of the absolutes.

    John,

    That last one is an important tip. Thanks a lot.

+ 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] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  2. [SOLVED] Loop till end of the sheet and loop back to first column again
    By smlim7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2012, 10:03 AM
  3. Loop to find cell, paste to new column. Turn 1 column into many.
    By d0tc0m in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2011, 09:51 AM
  4. Replies: 1
    Last Post: 05-06-2011, 03:08 AM
  5. Loop a column on Sheet1 and loop a row on Sheet2 to find a match
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 02:09 PM

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