+ Reply to Thread
Results 1 to 12 of 12

Do Loop Until Loop

Hybrid View

carsto Do Loop Until Loop 02-13-2007, 03:13 PM
raypayette That is a VBA quirk. Your... 02-13-2007, 03:26 PM
gfrantsen1961 You are also missing an EndIf. 02-16-2007, 02:28 PM
carsto Thanks for the help, but it’s... 02-16-2007, 04:02 PM
jasoncw Change LookAt:= xlPart to... 02-16-2007, 04:24 PM
  1. #1
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Do Loop Until Loop

    This works to locate the first instance found:

    Sub FindData()
    
    FindIT = InputBox("Find What?")
    
    On Error GoTo NotFound
    
    Sheets("Part Number Database").Select
    
        Cells.FIND(What:=FindIT, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
           .Activate
         
        MsgBox (ActiveCell.Value) 
    
    NotFound:
        MsgBox ("the entry cannot be found")
    
    End Sub
    I want to add a Loop to Loop until the it searches all the way thru and the first instance is found again, but I can't make it work

    Sub FindData()
    
    FindIT = InputBox("Find What?")
    
    On Error GoTo NotFound
    
    Sheets("Part Number Database").Select
    
    ‘Here I want to add something that defines the first address found, like:
    Found = Cells.FIND(What:=FindIT, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not Found Is Nothing Then
    FirstAdd = Found.Address
    
    
    ‘Now I want to loop though until the Data Found is the same as the
    'First Instance of the Data but my loop doesn’t work
    Do
        Cells.FIND(What:=FindIT, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
           .Activate
         
    ‘At this point I want to make the MsgBox change to Yes/No 
    ‘where Yes Exits the sub and No Loops thru again…but how to do it???
    	MsgBox (ActiveCell.Value), vbYesNo
                   If vbYes Then
                   Exit Sub
                   If vbNo Then
    Loop Until ActiveCell.Address = FirstAdd
    
    NotFound:
        MsgBox ("the entry cannot be found")
    
    End Sub
    I get an error Loop without Do

  2. #2
    Forum Contributor
    Join Date
    12-12-2005
    Posts
    667
    That is a VBA quirk. Your error relates to If:
    If vbYes Then
    Exit Sub
    Else
    End If
    Best regards,

    Ray

  3. #3
    Registered User
    Join Date
    01-19-2007
    Posts
    24
    You are also missing an EndIf.

  4. #4
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    Thanks for the help, but it’s just not happening for me

    The goal is to search Sheet Database Column A:A for text

    Upon finding the first instance:
    MsgBox (ActiveCell.Value), vbYesNoCancel

    If vbNo
    Loop through until the active cell (found cell) is
    the same Address as the first instance found
    Then provide a message box "you have run out of options, try again"
    OK Exits Sub

    If vbCancel
    Exit Sub

    If vbYes
    Copy & paste the row to UserForm Sheet


    I’ve tried every combination of If and Else, to no avail!


    Instead of Looping through every instance answering yes/no, a box containing all the instances and you select one of them would be good too. – actually BETTER!
    My problem with that is the FIND feature finds partial matches and matches not starting at the first character, and I don’t know any other way to search that way.
    Example: Searching on CAR finds:
    CAR
    CARZ
    BACAR


    Here’s the incomplete code

    Sub FindData()
    
    Dim FindIT As Variant
    FindIT = InputBox("Find What?")
    If FindIT = "" Then GoTo BoxEmpty
    
    On Error GoTo NotFound
    
    Do
        Sheets("Part Number Database").Select
        Columns("A:A").Select
        
        Cells.FIND(What:=FindIT, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
        .Activate
         
        MsgBox (ActiveCell.Value), vbYesNoCancel
            
            If vbNo Then
    Loop Until ActiveCell.Address = THE FIRST INSTANCE FOUND & MsgBox "you have run out of options, try again" & Exit Sub
    
            
            If vbCancel Then Exit Sub
    
            
            If vbYes Then ActiveCell.Select
          
            'after vbYes it copy's the family of part numbers to the User Form
            
                Set Family = Range(ActiveCell, ActiveCell.End(xlToRight))
                Family.Select
                Selection.Copy
                Sheets("UserForm").Select
                Range("B1").Select
                Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False, _
                    transpose:=True
                Application.CutCopyMode = False
                Exit Sub
    
    
    NotFound:
        MsgBox "the entry cannot be found"
        Exit Sub
        
    
    BoxEmpty:
        MsgBox "you must enter a value"
        Exit Sub
        
    End Sub

  5. #5
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Quote Originally Posted by carstowal
    My problem with that is the FIND feature finds partial matches and matches not starting at the first character, and I don’t know any other way to search that way.
    Example: Searching on CAR finds:
    CAR
    CARZ
    BACAR
    Change LookAt:= xlPart to LookAt:= xlWhole.

  6. #6
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    that is meaningless to me
    I'm only just starting learning to write VBA code.

+ 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