+ Reply to Thread
Results 1 to 7 of 7

For loop within loop not working correctly

  1. #1
    Registered User
    Join Date
    03-10-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    70

    For loop within loop not working correctly

    Ok, maybe "correctly" is the right word, but not as how I would like it to work.

    I have a sheet (sheet7) with data that I would like to loop through and export the values in that row if the value in column A is equal to something. The loop below is almost working, but instead of checking the value in column A, exporting the row, and then moving onto the next row, it just exports that row over and over in the table, and then exports the next correct row on top of that one over and over.

    Please Login or Register  to view this content.
    So it should go down column A of sheet 7, and for every Row where cell A&R = a sheet2 A2, then export the column D Row R to the table, and move onto the next row.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: For loop within loop not working correctly

    Are you sure it is right subforum?
    Use of ActiveDocument suggests you are dealing with Word rather than Excel.
    Anyway, as it is probably done right in the part of code you have not presented replace the part with:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: For loop within loop not working correctly

    Or, as column number variable (co) is used just once you can write 1 and 2 directly into each cell coordinates:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-10-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: For loop within loop not working correctly

    Thanks Kaper.

    It is an excel macro that creates a table in word, so a little bit of both?

    Now for the next task -

    I need it to go down the column in sheet6 instead of just cell A2.

    So something like this:

    Please Login or Register  to view this content.
    Basically, it needs to do exactly what you had, but instead of just for Cell A2, it's the range A2:A6

    Still learning - appreciate it.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: For loop within loop not working correctly

    Again - without sample file and BEFORE and AFTER snapshots not totally clear, but if
    1) you want to go sheet7 A1:A55 compare with sheet 6 A2 and list all Dx And Ex in table (x is the row between 1 and 55 where matches were found)
    2) then do the same with sheet 6 A3
    etc.
    then:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-10-2014
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: For loop within loop not working correctly

    Investment Allocation Template2.docTableMakerUpload.xlsm

    It didn't quite work. I've attached the 2 files.

    On sheet6 is a list of "plan codes". Each code has a certain amount of records attached to it, that can be found in sheet7. I need to populate a table in word for all of the records of the first plan found in sheet6 A2, and then save it as a word document, and then recreate the table and populate it with the records for the plan in sheet6 A3, and down the column.

    The code above populates the table with the records for one plan, and then just keeps repeating all of the records for only the bottom row for the other plans.


    Thanks

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: For loop within loop not working correctly

    move the code opening and closing + saving into the external loop
    Please Login or Register  to view this content.

+ 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. Loop not progressing correctly
    By Jietoh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-11-2015, 08:11 AM
  2. [SOLVED] Can't get nested Loop to copy range values from 2 tables working correctly
    By gtol in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2013, 12:01 PM
  3. LOOP not functioning correctly
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2013, 02:03 PM
  4. [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
  5. Run time Error 91 - End Loop not working correctly
    By rowing190 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-05-2009, 05:18 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