+ Reply to Thread
Results 1 to 7 of 7

For loop within loop not working correctly

Hybrid View

  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.

    NumFu = Application.CountIf(Sheet7.Range("A:A"), Sheet6.Range("A2").Value)   //// numfu is the number of times the value I'm checking for appears in column A of sheet 7. it will be the number of rows in the table
    
    Dim R As Integer, co As Integer, TR As Integer   ////R is the number of rows in sheet7 that it loops through, TR is rows in the table being exported, ///and co is the column of the table
        Set oTable = ActiveDocument.Tables.Add(ActiveDocument.Bookmarks("Table").Range, NumFu, 4)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For R = 1 To 55
          For TR = 1 To NumFu
            For co = 1 To 1
              If Sheet7.Range("A" & (R)) = Sheet6.Range("A2") Then
               oTable.Cell(TR, co).Range.Text = Sheet7.Range("D" & (R))
               Else
               End If
               Next
             Next  
            
            For co = 2 To 2
               If Sheet7.Range("A" & (R)) = Sheet6.Range("A2") Then
               oTable.Cell(TR, co).Range.Text = Sheet7.Range("E" & (R))
               Else
               
              End If
                Next
            Next
    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,879

    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:
    NumFu = Application.CountIf(Sheet7.Range("A:A"), Sheet6.Range("A2").Value) '  //// numfu is the number of times the value I'm checking for appears in column A of sheet 7. it will be the number of rows in the table
    Dim R As Integer, co As Integer, TR As Integer '  ////R is the number of rows in sheet7 that it loops through, TR is rows in the table being exported, ///and co is the column of the table
    Set oTable = ActiveDocument.Tables.Add(ActiveDocument.Bookmarks("Table").Range, NumFu, 4)
    oTable.Range.ParagraphFormat.SpaceAfter = 6
    ' changes start here 
    TR = 1
    For R = 1 To 55
      If Sheet7.Range("A" & (R)) = Sheet6.Range("A2") Then
        co = 1
        oTable.Cell(TR, co).Range.Text = Sheet7.Range("D" & (R))
        co = 2
        oTable.Cell(TR, co).Range.Text = Sheet7.Range("E" & (R))
        TR = TR + 1
      End If
    Next
    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,879

    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:
    TR = 1
    For R = 1 To 55
      If Sheet7.Range("A" & (R)) = Sheet6.Range("A2") Then
        oTable.Cell(TR, 1).Range.Text = Sheet7.Range("D" & (R))
        oTable.Cell(TR, 2).Range.Text = Sheet7.Range("E" & (R))
        TR = TR + 1
      End If
    Next

  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:

    PR = 1
    TR = 1
    For R = 1 To 55
      If Sheet7.Range("A" & (R)) = Sheet6.Range("A" & PR) Then
        oTable.Cell(TR, 1).Range.Text = Sheet7.Range("D" & (R))
        oTable.Cell(TR, 2).Range.Text = Sheet7.Range("E" & (R))
        TR = TR + 1
        PR = PR + 1
      End If
    Next
    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,879

    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:
    TR = 1
    For PR = 2 to 6
     For R = 1 To 55
      If Sheet7.Range("A" & (R)) = Sheet6.Range("A" & PR) Then
        oTable.Cell(TR, 1).Range.Text = Sheet7.Range("D" & (R))
        oTable.Cell(TR, 2).Range.Text = Sheet7.Range("E" & (R))
        TR = TR + 1
      End If
     Next R
    Next PR

  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,879

    Re: For loop within loop not working correctly

    move the code opening and closing + saving into the external loop
    For PR = 2 to 6
    'here the code to open word file create appropriate size structures etc
    
    TR = 1
    
     For R = 1 To 55
      If Sheet7.Range("A" & (R)) = Sheet6.Range("A" & PR) Then
        oTable.Cell(TR, 1).Range.Text = Sheet7.Range("D" & (R))
        oTable.Cell(TR, 2).Range.Text = Sheet7.Range("E" & (R))
        TR = TR + 1
      End If
     Next R
    
    ' here saveAs word file and close it
    
    Next PR

+ 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