none
My Loop Only Cycles Once RRS feed

  • Question

  • I have a large Macro of which one part does not work properly and I am missing where the problem is.  The code is:

    'Remove Duplicates and Transfer Ambiguous Corrections
    MsgBox ("Ready to Delete and Move")
    Documents(MasterAC_File).Activate 'extra
    
    'Find all Duplicates & Remove Those Rows
    
    With ActiveDocument.Content.Find
        .Text = "Remove"
        .Forward = True
        .Execute
        If .Found = True Then
            'Delete Row
            Selection.MoveRight Unit:=wdCharacter, Count:=3
            Selection.MoveUp Unit:=wdLine, Count:=1
            Selection.Rows.Delete
            count_Removed = count_Removed + 1
        End If
            
    End With
    
    Documents.Save NoPrompt:=True 'Save Master File
    
    MsgBox (count_Removed & " Duplicates Should Be Removed at this point and Ready for Saving.")

    The purpose is to find all rows of a table in a document with "Remove" in the 3rd column of many rows and delete those rows.

    When I run this code, it seems to <g class="gr_ gr_29 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="29" id="29">only</g> cycle once.  It seems to find the first instance of "Remove", deletes that row and breaks out of the loop despite the fact that there are many more rows that should be deleted.  Certainly, I must have coded something wrong, but I can't spot the error.  Any help would be appreciated.


    Lewis D. Eigen


    • Edited by LDEigen Saturday, May 5, 2018 10:35 PM
    Saturday, May 5, 2018 10:34 PM

Answers

  • Hello LDEigen,

    You did not do any loop in the code. There is no code could make it run in cycle.

    For your issue, since you could make sure all the "Remove" is in the 3rd column, I think looping the table rows will be more directly and easy to design the method.

    Please see below simply code.

    Dim objTable As Table
    Dim objRow As Row
    MsgBox ("Ready to Delete and Move")
    count_Removed = 0
    Application.ScreenUpdating = False
    For Each objTable In ActiveDocument.Tables
    For i = objTable.Rows.Count To 1 Step -1
    If InStr(objTable.Rows(i).Cells(3), "Remove") > 0 Then
    objTable.Rows(i).Delete
    count_Removed = count_Removed + 1
    End If
    Next i
    Next objTable
    Application.ScreenUpdating = True
    Documents.Save NoPrompt:=True 'Save Master File
    MsgBox (count_Removed & " Duplicates Should Be Removed at this point and Ready for Saving.")

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by LDEigen Monday, May 7, 2018 4:20 AM
    Monday, May 7, 2018 2:02 AM

All replies

  • Hello LDEigen,

    You did not do any loop in the code. There is no code could make it run in cycle.

    For your issue, since you could make sure all the "Remove" is in the 3rd column, I think looping the table rows will be more directly and easy to design the method.

    Please see below simply code.

    Dim objTable As Table
    Dim objRow As Row
    MsgBox ("Ready to Delete and Move")
    count_Removed = 0
    Application.ScreenUpdating = False
    For Each objTable In ActiveDocument.Tables
    For i = objTable.Rows.Count To 1 Step -1
    If InStr(objTable.Rows(i).Cells(3), "Remove") > 0 Then
    objTable.Rows(i).Delete
    count_Removed = count_Removed + 1
    End If
    Next i
    Next objTable
    Application.ScreenUpdating = True
    Documents.Save NoPrompt:=True 'Save Master File
    MsgBox (count_Removed & " Duplicates Should Be Removed at this point and Ready for Saving.")

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by LDEigen Monday, May 7, 2018 4:20 AM
    Monday, May 7, 2018 2:02 AM
  • Thanks so much.  I realize my error now.

    Lewis D. Eigen

    Monday, May 7, 2018 4:20 AM