+ Reply to Thread
Results 1 to 21 of 21

Vba loop help for cumulative addition

  1. #1
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18

    Vba loop help for cumulative addition

    Hi I require help in creating a loop procedure
    My data starts from cell a5
    The loop should start from cell b5, add values of b5 and c6, next add b5,c5,d5 to a different sheet until no of times I specify
    This should happen until last used row in a5
    Last edited by 6StringJazzer; 01-24-2019 at 01:44 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,834

    Re: Vba loop help

    Attach a file with sample data and a mockup of what you want the result to look like.

    What is your level of expertise in VBA?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18

    Re: Vba loop help for cumulative addition

    Hi,

    Please check below
    Capture.JPG

    1) Loop needs to start from D2, Add values of B2 and C2, check if D2 value is more than average of sum of B2 and C2, if more highlight in red
    2) Loop need to continue at E2 where it needs to add values of B2,C2,D2 and check average, if more highlight in red
    3) Loop need to stop before grand total in row

  4. #4
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18

    Re: Vba loop help for cumulative addition

    Hi any help on this?

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,834

    Re: Vba loop help for cumulative addition

    First I requested that you attach a file and you attached an image. We can't modify an image. The paper clip icon does not work for attachments. To attach a file, under the text box where you type your reply click the Go Advanced button. On the next screen scroll down and click on Manage Attachments, which will show a pop-up window to Select and Upload a file. Then close the window.

    Second, I asked about your level of expertise in VBA and you did not address that.

    Is this a pivot table?

  6. #6
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18

    Re: Vba loop help for cumulative addition

    Hi,

    Thanks for reply, attaching the file..

    Yes, info provided is sample data, but my actual data is from a pivot table.
    (i have copied pivot table contents to a different sheet through vba, if able to show info on actual pivot table that will be even helpful)
    My knowledge on vba is moderate..
    Attached Files Attached Files

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,078

    Re: Vba loop help for cumulative addition

    A couple of questions:

    A) How does the logic for the USA row work? You have only highlighted the first 4, but those initial 0s should pull the cumulative average down to where all of the entries in the USA row are highlighted. Your logic is not clear to me.

    B) Are you required to use VBA for this? It seems to me like it would be simpler to use conditional formatting with an appropriate formula. https://support.office.com/en-us/art...b-f1951ff89d7f In your sample spreadsheet I:
    1) Selected F5:I10
    2) Conditional formatting -> New Rule -> Use formula.
    3) Formula is: =F5>AVERAGE($D5:E5) [note the mix of relative and absolute references] Format is: Red text.
    That highlighted all the same values -- except for the USA row as mentioned above. Even if I am misunderstanding your desired logic, the same approach should work -- we just need to figure out exactly what formula logic you need.

    I guess the big question at this point if if you are required to use VBA for this, or if you can use conditional formatting.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18
    Hi MrShorty,

    This info is from pivottable, assuming there are weeks in column I would like to start from week 3, where week 3 needs to sum values of week 1 and 2. If week 3 value is more average of week 1 and 2 then week 3 should be red
    Continuing on week 4, this needs to add values from week1,2,3 and if week 4 is more than avg of these three week 4 should be red
    Like this goes on till last column
    This cannot be done with conditional formatting as my no of weeks might be 40 to 50,

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,834

    Re: Vba loop help for cumulative addition

    Quote Originally Posted by MrShorty View Post
    B) Are you required to use VBA for this? It seems to me like it would be simpler to use conditional formatting with an appropriate formula.
    That was my first thought, which is why I asked about the pivot table. IME if your pivot table changes the number of rows and columns, it wipes out any conditional formatting that had been applied.

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,078

    Re: Vba loop help for cumulative addition

    This info is from pivottable, assuming there are weeks in column I would like to start from week 3, where week 3 needs to sum values of week 1 and 2. If week 3 value is more average of week 1 and 2 then week 3 should be red
    Continuing on week 4, this needs to add values from week1,2,3 and if week 4 is more than avg of these three week 4 should be red
    Like this goes on till last column
    If I understand, this is not what you have done in the USA row:

    D column: average(0,0)=0 -> 4 is greater than 0 -> so text is red (which it is).
    E column: average(0,0,4)=1.3 -> 3 is greater than 1.3 -> text should be red, but you have not marked this as red. Why?
    F column: average(0,0,4,3)=1.75 -> 7 is greater than 1.75 -> text should be red, but you have not marked this as red. Why?
    G column: average(0,0,4,3,7)=2.8 -> 8 is greater than 2.8 -> text should be red, but you have not marked this as red. Why?

    Do you understand my confusion about the exact logic you are trying to use?

    I don't understand why a variable number of weeks would prohibit you from using conditional formatting. You should just need to expand the selection and include appropriate conditions. I:
    1) Selected F5:BD10 (assuming you would never need more than 52 weeks total)
    2) Conditional formatting -> New Rule -> Use formula.
    3) Formula is: =AND(F$4<>"Grand Total",F5>AVERAGE($D5:E5)) [note the mix of relative and absolute references] Format is: Red text.

    Again, this highlights all of the same numbers -- except for the misunderstanding around the USA row.

  11. #11
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18
    Quote Originally Posted by 6StringJazzer View Post
    That was my first thought, which is why I asked about the pivot table. IME if your pivot table changes the number of rows and columns, it wipes out any conditional formatting that had been applied.
    The data is copied to different sheet if that helps

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,078

    Re: Vba loop help for cumulative addition

    That was my first thought, which is why I asked about the pivot table. IME if your pivot table changes the number of rows and columns, it wipes out any conditional formatting that had been applied.
    I -- perhaps in error -- got the impression that it was copied from a pivot table but not trying to apply to the actual pivot table. That could change things.

    Edit: OP seems to have confirmed that he is trying to apply the formatting to a copy of the pivot table, not the actual pivot table.

  13. #13
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18
    Quote Originally Posted by MrShorty View Post
    If I understand, this is not what you have done in the USA row:

    D column: average(0,0)=0 -> 4 is greater than 0 -> so text is red (which it is).
    E column: average(0,0,4)=1.3 -> 3 is greater than 1.3 -> text should be red, but you have not marked this as red. Why?
    F column: average(0,0,4,3)=1.75 -> 7 is greater than 1.75 -> text should be red, but you have not marked this as red. Why?
    G column: average(0,0,4,3,7)=2.8 -> 8 is greater than 2.8 -> text should be red, but you have not marked this as red. Why?

    Do you understand my confusion about the exact logic you are trying to use?

    I don't understand why a variable number of weeks would prohibit you from using conditional formatting. You should just need to expand the selection and include appropriate conditions. I:
    1) Selected F5:BD10 (assuming you would never need more than 52 weeks total)
    2) Conditional formatting -> New Rule -> Use formula.
    3) Formula is: =AND(F$4<>"Grand Total",F5>AVERAGE($D5:E5)) [note the mix of relative and absolute references] Format is: Red text.

    Again, this highlights all of the same numbers -- except for the misunderstanding around the USA row.
    Hi sorry,
    You are right those should have been red,
    Wrt above conditional formatting do I need to write this code for every column?

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,078

    Re: Vba loop help for cumulative addition

    As noted, I selected the entire 50 column block, so I did not need to do this for each column separately. A lot of times, if you pay careful attention to relative and absolute referencing, your formula/format can apply to the entire range. That is why I tried to draw attention to how I used relative and absolute references for this formula.

  15. #15
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18
    Quote Originally Posted by MrShorty View Post
    As noted, I selected the entire 50 column block, so I did not need to do this for each column separately. A lot of times, if you pay careful attention to relative and absolute referencing, your formula/format can apply to the entire range. That is why I tried to draw attention to how I used relative and absolute references for this formula.
    Will check and update thank you very much
    Also is it possible to provide a loop formula for the above scenario
    Last edited by mahadeva_raobc; 01-25-2019 at 03:22 PM.

  16. #16
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,078

    Re: Vba loop help for cumulative addition

    I'm not sure I understand what you mean by "loop formula". Are you saying you still want to see how this would be done in VBA?

  17. #17
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18
    Here is the link where there is no reply from that forum
    https://www.mrexcel.com/forum/excel-...light=Vba+loop
    Last edited by 6StringJazzer; 01-26-2019 at 09:42 AM.

  18. #18
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,834

    Re: Vba loop help for cumulative addition

    We are all confused by your description of what you are trying to do. I have attached a file with two versions, one using VBA and one using conditional formatting.
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18

    Re: Vba loop help for cumulative addition

    Hi,

    Thank you very much the vba part worked for me...

  20. #20
    Registered User
    Join Date
    01-28-2015
    Location
    bangalore
    MS-Off Ver
    MS Office 2010
    Posts
    18

    Re: Vba loop help for cumulative addition

    Hi @6StringJazzer,

    The vba part is working,Thank you..
    I would like to know is it possible in the above dataset to omit blank and zero in a row and then calculate average only for cells which has a number?

  21. #21
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    16,078

    Re: Vba loop help for cumulative addition

    It seems like it should be as simple as replacing the AVERAGE() function with an AVERAGEIFS() function. Using my conditional formatting formula from post #10 as an example: =AND(F$4<>"Grand Total",F5>AVERAGEIFS($D5:E5,$D5:E5,">0")) Or am I misunderstanding something that makes it more difficult than an AVERAGEIFS() function?

+ 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] Find and Replace Loop + a Loop to remove Duplicate values
    By lougs7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2018, 07:43 AM
  2. Replies: 13
    Last Post: 07-08-2018, 05:22 AM
  3. [SOLVED] (Beginner help) For loop inside do loop that displays information from reference sheet.
    By lediable007 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-08-2016, 03:20 PM
  4. [SOLVED] Auto email loop with formatted table data breaking on second loop VBA
    By Who_else in forum Excel General
    Replies: 6
    Last Post: 03-24-2016, 06:22 AM
  5. [SOLVED] VBA to loop within a loop of multiple blocks of data (currentregion) ... Expert needed
    By Jim885 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2016, 08:28 PM
  6. [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
  7. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 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