+ Reply to Thread
Results 1 to 6 of 6

Loop For Each

  1. #1
    Registered User
    Join Date
    09-03-2019
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    3

    Loop For Each

    Hi everyone,

    I am trying to loop the range of formulas, that are located on one row and as a result i want to see a table which will include results of this formula based on parameters.

    To tell the story, i have 3 tabs in excel, summary, parameters and drill down,

    In parameters sheet, i put information about each contract so i have lets say 10 contract ID in each column and on rows i have specific parameters for those contracts.
    Drill down takes the parameters and does calculation for each contract. (This is looped via For Each) So in one cell it picks the contract ID one by one and does the calculation for each one.
    On Summary sheet, i want to transfer the results of those calculation also one by one, but instead of overwriting the results in one range, i want to have a table, which will have contract IDs in rows and some results in columns.
    I guess i need to still use For Each loop, and i even indicated the range for the Contract IDs to be not one cell, but an actual range, but it still overwrites it all in one cell.
    Here is the code,
    Can you help?

    Please Login or Register  to view this content.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop For Each

    This question really should be a FAQ. I see it time and time again and looping just isn't the best way of doing this sort of thing.

    Whenever you find yourself creating looping code stop and think is this really necessary? Most of the time it isn't. Loops are inherently slow when they have to jump back and forth between VBA and the Excel application - there's a big time overhead at each pass through the loop.

    The fastest, most efficient and elegant way for this sort of stuff is to use an Autofilter to filter the rows you're interested in.
    Then use syntax like

    Please Login or Register  to view this content.
    then paste to your destination cell.

    This uses just one Copy / Paste instruction rather than many if you're looping through many cells.

    So use the column that contains your 'singlecontract' references and Autofilter it for Non Blanks then copy and paste all the visible cells. Then finish by removing the Autofilter.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    09-03-2019
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    3

    Re: Loop For Each

    Hi Richard,

    Thank you!

    And Sorry, This is literally the second time in my life i am trying to do something using VBA and looping or autofilter or basically anything related to any kind of coding is something, i have never done before.
    So, pardon my really basic questions.

    I will get myself familiar with autofilter.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop For Each

    Upload your workbook if you need more specific help.

  5. #5
    Registered User
    Join Date
    09-03-2019
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    3

    Re: Loop For Each

    i would love to, but i can't upload it as the button for upload does not work, and i do not have enough posts to put the link.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop For Each

    To upload a file click the Go Advanced button at the foot of your post, look underneath the post area for the Manage Attachments section and take it from there.

    Be sure to show a before and after situation with manually calculated results, explaining which information is data and which is results, and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

+ 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] VBA to loop through cell-defined range and paste table output after each loop.
    By mscott123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-18-2019, 03:41 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

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