+ Reply to Thread
Results 1 to 11 of 11

Infinite Loop

Hybrid View

  1. #1
    Registered User
    Join Date
    09-11-2007
    Posts
    41

    Infinite Loop

    hello all...

    i'm trying to loop through a bunch of worksheets and copy certain cells and paste it in a master sheet.

    Please take a look at my code below because it keeps copying the same thing over and over and is stuck in an infinite loop.

    For Each ws In ActiveWorkbook.Worksheets
        
        If ws.Visible = True Then
            ws.Activate
    
            Set rFind = Cells.Find(What:="Critical Activities / Watch List Items / and Milestones for Next 3 Months", After:=[A1], _
                                    LookIn:=xlValues, _
                                    lookat:=xlPart, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False, _
                                    SearchFormat:=False)
            If Not rFind Is Nothing Then
            'if it's the first sheet, copy the column headers, if not skip the headers
                If ActiveSheet.name = "A1 Replace Citichecking" Then
                    currentRow = rFind.Row + 1
                Else
                    currentRow = rFind.Row + 2
                End If
                'copy all rows until empty line, insert date and name
                'Do While Not IsEmpty(Cells(currentRow, 2))
                Do While (Cells(currentRow, 2) <> "Issues/Risks Log")
                    Select Case Cells(currentRow, 2)
                        Case Is = ""
                            currentRow = currentRow + 1
                        Case Is = "Major Milestones to Program End Date Beyond Current 3 Month Cycle (Maximum 10 Items)"
                            currentRow = currentRow + 1
                        Case Is = "26. Responsible Area / Owner"
                            currentRow = currentRow + 1
                        Case Else
                            Set rFindw = ActiveSheet.UsedRange.Find("Program Id", LookIn:=xlValues, lookat:=xlWhole)
                            currentRow = rFindw.Row + 1
                            strprogID = Range("K" & currentRow).Value
                            strcritical = "BCM"
                            strMileName = Range("C" & currentRow).Value
                            strMileStatus = Range("I" & currentRow).Value
                            strReason = Range("M" & currentRow).Value
                            strDescr = Range("D" & currentRow).Value
                            dtOrig = Range("G" & currentRow).Value
                            dtActual = Range("H" & currentRow).Value
                            strCommit = Range("L" & currentRow).Value
                            strMileCmmt = Range("N" & currentRow).Value
                            strRemCmmt = Range("O" & currentRow).Value
                            
                                If rFindw Is Nothing Then
                                    MsgBox "Not Found"
                                    Exit Sub
                                End If
                            dest.Range("A" & cursor).Value = strprogID
                            dest.Range("B" & cursor).Value = strcritical
                            dest.Range("C" & cursor).Value = strMileName
                            dest.Range("D" & cursor).Value = strMileStatus
                            dest.Range("E" & cursor).Value = strReason
                            dest.Range("F" & cursor).Value = strDescr
                            'dest.Range("F" & cursor).EntireColumn.WrapText = True
                            dest.Range("G" & cursor).Value = dtOrig
                            dest.Range("H" & cursor).Value = dtActual
                            dest.Range("I" & cursor).Value = strCommit
                            dest.Range("J" & cursor).Value = strMileCmmt
                            dest.Range("K" & cursor).Value = strRemCmmt
                            dest.Range("L" & cursor).Value = ActiveSheet.name
                            dest.Range("M" & cursor).Value = name
                            dest.Range("N" & cursor).Value = addDate
                                                    
                            
                            cursor = cursor + 1
                            currentRow = currentRow + 1
                        End Select
                Loop
    
            End If
        End If
    Next ws
    
    
    Worksheets("BCMs").Select
    'Delete columns GH in destination worksheet to fix merge issue
    Columns("G:H").Delete Shift:=xlToLeft
    Columns("J").Delete (xlToLeft)
    Thank you.
    Last edited by phong919; 01-22-2010 at 04:57 PM.

  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: Infinite Loop help

    Hi,

    With 35 posts you really should know the rules about embedding code between Code tags.

    Please correct.

    Rgds
    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-11-2007
    Posts
    41

    Re: Infinite Loop

    sorry about that. code corrected.

  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: Infinite Loop

    Hi,

    It would be easier to advise with certainty if we had the actual workbook since the loop exit condition in the line below is clearly never being met.

      Do While (Cells(currentRow, 2) <> "Issues/Risks Log")
    Try stepping through the macro to observe why the Do never encounters a value in the 2nd (presumably B) column which is not equal to "Issues/Risks Log"

    For instance are the 'Issues/Risks Log' in a different case perhaps, spelled differently or perhaps have a leading/trailing space?

    If you can't debug it, upload the workbook, anonymised if necessary and limited to a representative sample of rows if there are hundreds of them.

    Regards
    Rgds

  5. #5
    Registered User
    Join Date
    09-11-2007
    Posts
    41

    Re: Infinite Loop

    Here is the attached workbook.

    I just checked for the trailing spaces in the do while and it doesn't have any.
    Attached Files Attached Files

  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: Infinite Loop

    Hi

    In your Case Else statement you are resetting the currentRow variable in the 2nd line to the value 28 i.e.

    Case Else
                            Set rFindw = ActiveSheet.UsedRange.Find("Program Id", LookIn:=xlValues, lookat:=xlWhole)
                            currentRow = rFindw.Row + 1
    So even though you are adding +1 at the end of the Else statement the currentrow variable never reaches the value 37 which is the Loop exit condition.

    Rgds

  7. #7
    Registered User
    Join Date
    09-11-2007
    Posts
    41

    Re: Infinite Loop

    what would be the best way of doing it. i removed the

    currentRow = rFindw.Row + 1

    and it doesn't run correctly. The loop takes the actual header on line 28 and not the value in line 29.

    Thanks

  8. #8
    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: Infinite Loop

    Hi,

    Commenting out that line seems to work fine on the copy I have. The currentrow variable processes until it reaches the value 37 where it exits the Do..Loop and goes on to the next Worksheet if there are any.

    What makes you think it's not working? Have you tried stepping through it line by line to observe what happens?

    Regards

  9. #9
    Registered User
    Join Date
    09-11-2007
    Posts
    41

    Re: Infinite Loop

    Yeah i did the same and it loops through the data but it's missing some column values that i don't see. maybe there's something wrong with my excel application?

  10. #10
    Registered User
    Join Date
    09-11-2007
    Posts
    41

    Re: Infinite Loop

    i'm missing all the column values for

    dest.Range("L" & cursor).Value = ActiveSheet.name

    at the end of the loop.

  11. #11
    Registered User
    Join Date
    09-11-2007
    Posts
    41

    Re: Infinite Loop

    actually i figured it out. there was a couple of columns being deleted towards the end. DUH! thank you for your assistance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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