+ Reply to Thread
Results 1 to 12 of 12

Do Loop Until Loop

Hybrid View

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

  2. #2
    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

  3. #3
    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.

  4. #4
    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.

  5. #5
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    In the FIND function, using LookAt:=xlPart will create a match if the search string is located anywhere within the cell (i.e. it finds the match in "part" of the cell). Using LookAt:=xlWhole will only create a match if the "whole" cell matches the search string. Make sense?

    Ok, I also played around with your code a little and came up with this. Let us know if it works as intended.

    Sub FindData()
    
    Dim FindIT As Variant
    Dim response, frstMatch, Family As Range
    
    Do
        FindIT = InputBox("Find What?")
    
        If StrPtr(FindIT) = 0 Then  'Cancel was pressed
            Exit Sub
    
        ElseIf Len(FindIT) = 0 Then  'OK pressed but nothing entered
            MsgBox "You must enter a value."
            
        End If
    
    Loop
    
    On Error GoTo NotFound
    
    Sheets("Part Number Database").Select
    Columns("A:A").Select
    
    Cells.Find(What:=FindIT, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
        .Activate
        
    frstMatch = ActiveCell.Address
    
        Do
        
            response = MsgBox(ActiveCell.Value, vbYesNoCancel)
        
                If response = vbCancel Then
                    Exit Sub
            
                ElseIf response = vbYes Then
          
                'after vbYes it copies the family of part numbers to the UserForm sheet
            
                    Set Family = Range(ActiveCell, ActiveCell.End(xlToRight))
                    Family.Copy
                    Sheets("UserForm").Range("B1").PasteSpecial Paste:=xlAll, _
                        Operation:=xlNone, SkipBlanks:=False, Transpose:=True
                    Application.CutCopyMode = False
                    Exit Sub
                
                End If
            
            Cells.Find(What:=FindIT, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
                xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
                .Activate
            
        Loop Until ActiveCell.Address = frstMatch
    
    
    MsgBox "You have run out of options.  Try again."
    
    Exit Sub
    
    NotFound:
        MsgBox "The entry cannot be found."
        Exit Sub
       
    End Sub
    Last edited by jasoncw; 02-16-2007 at 08:05 PM.

  6. #6
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    Thank a million Jason!

    It works great with one small thing that needs corrected.
    I ONLY want it to search in Column A

    Sheets("Part Number Database").Select
    Columns("A:A").Select

    Doesn't seem to be forcing it to only look in Column A, consequently it loops thru a LOT of options, if you keep saying NO!


    Also, right now, it's finding one potential match at a time and asking Yes or No.
    Is it possible to find all the potential matches on the page at once, and either copy them to another worksheet or pop them into a ListBox for scrolling through?

  7. #7
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Quote Originally Posted by carstowal
    Thank a million Jason!

    It works great with one small thing that needs corrected.
    I ONLY want it to search in Column A

    Sheets("Part Number Database").Select
    Columns("A:A").Select

    Doesn't seem to be forcing it to only look in Column A, consequently it loops thru a LOT of options, if you keep saying NO!
    Oops, sorry about that. There are 2 different parts in the code that state "Cells.Find". You can just change both of these to "Selection.Find" and this issue will be cured.

    Quote Originally Posted by carstowal
    Also, right now, it's finding one potential match at a time and asking Yes or No.
    Is it possible to find all the potential matches on the page at once, and either copy them to another worksheet or pop them into a ListBox for scrolling through?
    Sorry, it was my impression that is what you wanted. What would you like to see listed for multiple finds, the cell address, or other data?

+ 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