+ Reply to Thread
Results 1 to 7 of 7

do - loop until won't stop going back to beginning

Hybrid View

Dabbler39 do - loop until won't stop... 05-09-2013, 10:58 PM
protonLeah Re: do - loop until won't... 05-10-2013, 12:50 AM
Dabbler39 Re: do - loop until won't... 05-10-2013, 09:19 AM
Dabbler39 Re: do - loop until won't... 05-10-2013, 09:49 PM
Dabbler39 Re: do - loop until won't... 05-10-2013, 09:46 PM
Leith Ross Re: do - loop until won't... 05-10-2013, 12:59 AM
protonLeah Re: do - loop until won't... 05-11-2013, 01:25 AM
  1. #1
    Registered User
    Join Date
    12-03-2012
    Location
    Omaha, Nebraska
    MS-Off Ver
    2010
    Posts
    39

    do - loop until won't stop going back to beginning

    If IND.Value = True And IASUB = True Then
        If Trim(NAM) <> "" Then
            With Sheets("Initial Setup").Range("p6002:r13999")
                
                Set rng = .Find(What:=NAM, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not rng Is Nothing Then
                    Do
                    addr = rng.Address
                    addr1 = rng.Offset(0, -4)
                    addr2 = rng.Offset(0, -2)
                    addr3 = rng.Offset(0, -6)
                    a = MsgBox("There is a " & rng & ", " & Format(addr1, "(###) ###-####") & " in " & addr3 & ", " & ". Would You like to use this one?", vbYesNoCancel)
                    If a = vbCancel Then
                        GoTo 1000
                    Else
                    If a = vbYes Then
                        ws.Activate
                        If Range("d57").Value <> "" Then
                            If Range("d61").Value <> "" Then
                                b = MsgBox("There are no Open IA slots on this claim.  Would you like to replace one?", vbYesNoCancel)
                                If b = vbCancel Then
                                    GoTo 1000
                                Else
                                If b = vbNo Then
                                    MsgBox ("Unable to add IA.  Both slots are taken.  Consider removing one that you are not using anymore.")
                                    GoTo 1000
                                Else
                                If b = vbYes Then
                                    c = MsgBox("Would you like to replace IA #1?", vbYesNoCancel)
                                    If c = vbCancel Then
                                        GoTo 1000
                                    Else
                                    If c = vbNo Then
                                        Range("d61").Value = rng
                                        Range("d62").Value = addr1
                                        Range("d63").Value = addr2
                                        Range("d60").EntireRow.Hidden = False
                                        Range("d61").EntireRow.Hidden = False
                                        Range("d62").EntireRow.Hidden = False
                                        Range("d63").EntireRow.Hidden = False
                                        GoTo 1000
                                    Else
                                    If c = vbYes Then
                                        Range("d57").Value = rng
                                        Range("d58").Value = addr1
                                        Range("d59").Value = addr2
                                        Range("d56").EntireRow.Hidden = False
                                        Range("d57").EntireRow.Hidden = False
                                        Range("d58").EntireRow.Hidden = False
                                        Range("d59").EntireRow.Hidden = False
                                        GoTo 1000
                                    End If
                                    End If
                                    End If
                                End If
                                End If
                                End If
                            Else
                            If Range("d61") = "" Then
                                Range("d61").Value = rng
                                Range("d62").Value = addr1
                                Range("d63").Value = addr2
                                Range("d60").EntireRow.Hidden = False
                                Range("d61").EntireRow.Hidden = False
                                Range("d62").EntireRow.Hidden = False
                                Range("d63").EntireRow.Hidden = False
                                GoTo 1000
                            End If
                            End If
                        Else
                        If Range("d57") = "" Then
                            Range("d57").Value = rng
                            Range("d58").Value = addr1
                            Range("d59").Value = addr2
                            Range("d56").EntireRow.Hidden = False
                            Range("d57").EntireRow.Hidden = False
                            Range("d58").EntireRow.Hidden = False
                            Range("d59").EntireRow.Hidden = False
                            GoTo 1000
                        End If
                        End If
                        GoTo 1000
                    Else
                    If a = vbNo Then
                    End If
                    End If
                    End If
                    Set rng = .FindNext(rng)
                    Loop Until rng.Address = addr
                    GoTo 200

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,935

    Re: do - loop until won't stop going back to beginning

    All those "GoTo" 's are a problem; you didn't post the entire code.

    To Attach a Workbook:
    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    12-03-2012
    Location
    Omaha, Nebraska
    MS-Off Ver
    2010
    Posts
    39

    Re: do - loop until won't stop going back to beginning

    Thank you Proton. I am at work now, so can not access my files. However, I am probably going to try Leith's suggestion first. I am curious, however, as to what you see as a problem with the goto's. My goto's are working fine and my code works fine with the exception of the aforementioned issue. That being said, are you suggesting a better way of peeling the apple with the goto's? As I am a newb, I am always open to suggestions!!! If we're not learning, we're dead. I will upload my file when I get home tonight. Thank you for your help.

  4. #4
    Registered User
    Join Date
    12-03-2012
    Location
    Omaha, Nebraska
    MS-Off Ver
    2010
    Posts
    39

    Re: do - loop until won't stop going back to beginning

    I was unable to insert the entire code, but i cut out the section that fails. What's interesting is that the other section is a mirror of this code and works perfectly. It exits the do loop properly. Thanks all. P.S. I tried Leith's suggestion to no avail.

    Private Sub OK_Click()
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim LastRow As Long
    Dim rng As Range, rang As Range
    Dim addr As String
    Set ws = ThisWorkbook.ActiveSheet
    Set ws1 = ThisWorkbook.Sheets("Initial Setup")
    
    ScreenUpdating = False
    
    ws1.Activate
    1
    If IND.Value = True And IASUB = True Then
        If Trim(NAM) <> "" Then
            With Sheets("Initial Setup").Range("p6002:r13999")
                
                Set rang = .Find(What:=NAM, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not rang Is Nothing Then
                    Do
                    addr = rang.Address
                    addr1 = rang.Offset(0, -4)
                    addr2 = rang.Offset(0, -2)
                    addr3 = rang.Offset(0, -6)
                    a = MsgBox("There is a " & rang & ", " & Format(addr1, "(###) ###-####") & " in " & addr3 & ", " & ". Would You like to use this one?", vbYesNoCancel)
                    If a = vbCancel Then
                        GoTo 1000
                    Else
                    If a = vbYes Then
                        ws.Activate
                        If Range("d57").Value <> "" Then
                            If Range("d61").Value <> "" Then
                                b = MsgBox("There are no Open IA slots on this claim.  Would you like to replace one?", vbYesNoCancel)
                                If b = vbCancel Then
                                    GoTo 1000
                                Else
                                If b = vbNo Then
                                    MsgBox ("Unable to add IA.  Both slots are taken.  Consider removing one that you are not using anymore.")
                                    GoTo 1000
                                Else
                                If b = vbYes Then
                                    c = MsgBox("Would you like to replace IA #1?", vbYesNoCancel)
                                    If c = vbCancel Then
                                        GoTo 1000
                                    Else
                                    If c = vbNo Then
                                        Range("d61").Value = rang
                                        Range("d62").Value = addr1
                                        Range("d63").Value = addr2
                                        Range("d60").EntireRow.Hidden = False
                                        Range("d61").EntireRow.Hidden = False
                                        Range("d62").EntireRow.Hidden = False
                                        Range("d63").EntireRow.Hidden = False
                                        GoTo 1000
                                    Else
                                    If c = vbYes Then
                                        Range("d57").Value = rang
                                        Range("d58").Value = addr1
                                        Range("d59").Value = addr2
                                        Range("d56").EntireRow.Hidden = False
                                        Range("d57").EntireRow.Hidden = False
                                        Range("d58").EntireRow.Hidden = False
                                        Range("d59").EntireRow.Hidden = False
                                        GoTo 1000
                                    End If
                                    End If
                                    End If
                                End If
                                End If
                                End If
                            Else
                            If Range("d61") = "" Then
                                Range("d61").Value = rang
                                Range("d62").Value = addr1
                                Range("d63").Value = addr2
                                Range("d60").EntireRow.Hidden = False
                                Range("d61").EntireRow.Hidden = False
                                Range("d62").EntireRow.Hidden = False
                                Range("d63").EntireRow.Hidden = False
                                GoTo 1000
                            End If
                            End If
                        Else
                        If Range("d57") = "" Then
                            Range("d57").Value = rang
                            Range("d58").Value = addr1
                            Range("d59").Value = addr2
                            Range("d56").EntireRow.Hidden = False
                            Range("d57").EntireRow.Hidden = False
                            Range("d58").EntireRow.Hidden = False
                            Range("d59").EntireRow.Hidden = False
                            GoTo 1000
                        End If
                        End If
                        GoTo 1000
                    Else
                    If a = vbNo Then
                    End If
                    End If
                    End If
                    Set rang = .FindNext(rang)
                    Loop Until rang.Address = addr
                    GoTo 200
                
                Else
                If rng Is Nothing Then
    200             bb = MsgBox("This Individual will be added to the database for future use. Is that okay?", vbYesNoCancel)
                    If bb = vbNo Or Cancel Then
                        GoTo 1000
                    End If
                    LastRow = Range("p6002:r13999").End(xlDown).Row + 1
                    Cells(LastRow, 1).Value = COM.Value
                    Cells(LastRow, 4).Value = TAX.Value
                    Cells(LastRow, 6).Value = ADD.Value
                    Cells(LastRow, 9).Value = CIT.Value
                    Cells(LastRow, 10).Value = STA.Value
                    Cells(LastRow, 11).Value = ZIP.Value
                    Cells(LastRow, 12).Value = PHO.Value
                    Cells(LastRow, 14).Value = EMA.Value
                    Cells(LastRow, 16).Value = NAM.Value
                    Cells(LastRow, 19).Value = "IA"
                    ws.Activate
                    If Range("d57").Value <> "" Then
                        If Range("d61").Value <> "" Then
                            b = MsgBox("There are no Open IA slots on this claim.  Would you like to replace one?", vbYesNoCancel)
                            If b = vbCancel Then
                                GoTo 1000
                            Else
                            If b = vbNo Then
                                MsgBox ("Unable to add IA.  Both slots are taken.  Consider removing one that you are not using anymore.")
                                GoTo 1000
                            Else
                            If b = vbYes Then
                                c = MsgBox("Would you like to replace IA #1?", vbYesNoCancel)
                                If c = vbCancel Then
                                    GoTo 1000
                                Else
                                If c = vbNo Then
                                    Range("d61").Value = NAM
                                    Range("D62").Value = PHO
                                    Range("D63").Value = EMA
                                        
                                Else
                                If c = vbYes Then
                                    Range("d57").Value = NAM
                                    Range("d58").Value = PHO
                                    Range("d59").Value = EMA
                                    Range("d56").EntireRow.Hidden = False
                                    Range("d57").EntireRow.Hidden = False
                                    Range("d58").EntireRow.Hidden = False
                                    Range("d59").EntireRow.Hidden = False
                                End If
                                End If
                                End If
                            End If
                            End If
                            End If
                        Else
                        If Range("d61") = "" Then
                            Range("d61").Value = NAM
                            Range("d62").Value = PHO
                            Range("d63").Value = EMA
                            Range("d60").EntireRow.Hidden = False
                            Range("d61").EntireRow.Hidden = False
                            Range("d62").EntireRow.Hidden = False
                            Range("d63").EntireRow.Hidden = False
                        End If
                        End If
                    Else
                    If Range("d57") = "" Then
                        Range("d57").Value = NAM
                        Range("d58").Value = PHO
                        Range("d59").Value = EMA
                        Range("d56").EntireRow.Hidden = False
                        Range("d57").EntireRow.Hidden = False
                        Range("d58").EntireRow.Hidden = False
                        Range("d59").EntireRow.Hidden = False
                    End If
                    End If
                End If
                End If
            End With
        End If
    End If
    500
        
    1000
    ScreenUpdating = True
    Unload Me
    1001
    End Sub

  5. #5
    Registered User
    Join Date
    12-03-2012
    Location
    Omaha, Nebraska
    MS-Off Ver
    2010
    Posts
    39

    Re: do - loop until won't stop going back to beginning

    I was unable to upload the excel workbook due to it's size (forum prohibited). As such, I have inserted the entire code for this particular userform.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: do - loop until won't stop going back to beginning

    Hello Dabbler39
    ,
    Welcome to the Forum!

    When using the FindNext method of a Range, there are 2 conditions you must check for to terminate the Do loop. First is the Range found something. This is confirmed by testing if the Range is equal to the special object value Nothing. Second is to compare the address is not equal to the address of the first match.

    You have done the latter (test the address) but not the former (test for Nothing). The last coupe of lines should be...
                    Set rng = .FindNext(rng)
                    If rng Is Nothing Then Exit Do    ' <--- Change this to jump where you want to contnue when the last match is made.
                    Loop Until rng.Address = addr
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,935

    Re: do - loop until won't stop going back to beginning

    Without the whole code the below is only an example to eliminate the goto's.

    Private Sub OK_Click()
        Dim ACTVSHT     As Worksheet, _
            SETUPSHT    As Worksheet, _
            LastRow     As Long, _
            Rng         As Range, _
            Rang        As Range, _
            Addr        As String
        
        Set ACTVSHT = ThisWorkbook.ActiveSheet
        Set SETUPSHT = ThisWorkbook.Sheets("Initial Setup")
        
        ScreenUpdating = False
        
        If IND.Value = True And IASUB = True And Trim(NAM) <> "" Then
            With SETUPSHT.Range("p6002:r13999")
                Set Rang = .Find(What:=NAM, _
                                LookIn:=xlValues, _
                                LookAt:=xlPart)
    '---------------------------------------------------------------------------------------------
                If Not Rang Is Nothing Then
                    Do
                        Addr = Rang.Address
                        addr1 = Rang.Offset(0, -4).Value
                        addr2 = Rang.Offset(0, -2).Value
                        addr3 = Rang.Offset(0, -6).Value
                        UseNAM = MsgBox("There is UseNAM " & Rang & ", " & Format(addr1, "(###) ###-####") & " in " & addr3 & ", " & ". Would You like to use this one?", vbYesNoCancel)
                        
                        Select Case UseNAM
                            Case Is = vbYes
                                If ACTVSHT.Range("d57").Value <> "" And ACTVSHT.Range("d61").Value <> "" Then
                                    ReplaceIA = MsgBox("There are no Open IA slots on this claim.  Would you like to replace one?", vbYesNoCancel)
                                    Select Case ReplaceIA
                                        Case Is = vbNo
                                            MsgBox ("Unable to add IA.  Both slots are taken.  Consider removing one that you are not using anymore.")
                                            Exit Do
                                            
                                        Case Is = vbYes
                                            ReplaceIA_1 = MsgBox("Would you like to replace IA #1?", vbYesNoCancel)
                                            
                                            Select Case ReplaceIA_1
                                                Case Is = vbNo
                                                    With ACTVSHT
                                                        .Range("d61:d63").Value = Array(Rang, addr1, addr2)
                                                        .Range("d60:d63").EntireRow.Hidden = False
                                                    End With
                                                Case Is = vbYes
                                                    With ACTVSHT
                                                        .Range("d57:d59").Value = Array(Rang, addr1, addr2)
                                                        .Range("d56:d59").EntireRow.Hidden = False
                                                    End With
                                            End Select  'ReplaceIA_1
                                    End Select  'ReplaceIA
                        End Select  'UseNAM
                        Set Rang = .FindNext(Rang)
                    Loop While Not Rang Is Nothing And Rang.Address <> Addr
    '==============================================================================
                Else    'rang is nothing
                    NewRec = MsgBox("This Individual will be added to the database for future use. Is that okay?", vbYesNoCancel)
                    If NewRec = vbYes Then
                        With ACTVSHT
                            LastRow = .Range("p6002:r13999").End(xlDown).Row + 1
                            .Cells(LastRow, 1).Value = COM.Value
                            .Cells(LastRow, 4).Value = TAX.Value
                            .Cells(LastRow, 6).Value = Add.Value
                            .Cells(LastRow, 9).Value = CIT.Value
                            .Cells(LastRow, 10).Value = STA.Value
                            .Cells(LastRow, 11).Value = ZIP.Value
                            .Cells(LastRow, 12).Value = PHO.Value
                            .Cells(LastRow, 14).Value = EMA.Value
                            .Cells(LastRow, 16).Value = NAM.Value
                            .Cells(LastRow, 19).Value = "IA"
                        End With    'ACTVSHT
                        If ACTVSHT.Range("d57").Value <> "" And ACTVSHT.Range("d61").Value <> "" Then
                            ReplaceIA = MsgBox("There are no Open IA slots on this claim.  Would you like to replace one?", vbYesNoCancel)
                            
                            Select Case ReplaceIA
                                Case Is = vbNo
                                    MsgBox ("Unable to add IA.  Both slots are taken.  Consider removing one that you are not using anymore.")
                                Case Is = vbYes
                                    RelplaceIA_1 = MsgBox("Would you like to replace IA #1?", vbYesNoCancel)
                                    
                                    Select Case RelplaceIA_1
                                        Case Is = vbNo
                                            ACTVSHT.Range("d61:D63").Value = Array(NAM, PHO, EMA)
                                        Case Is = vbYes
                                           ACTVSHT.Range("d57:d59").Value = Array(NAM, PHO, EMA)
                                           ACTVSHT.Range("d56:d59").EntireRow.Hidden = False
                                    End Select
                            End Select
                        Else
                            If Range("d61") = "" Then
                                ACTVSHT.Range("d61:D63").Value = Array(NAM, PHO, EMA)
                            End If
                            If Range("d57") = "" Then
                                ACTVSHT.Range("d57:d59").Value = Array(NAM, PHO, EMA)
                                ACTVSHT.Range("d56:d59").EntireRow.Hidden = False
                            End If
                        End If
                    End If
            End With    'SETUPSHT
        End If  'IND.VALUE
        ScreenUpdating = True
        Unload Me
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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