Consulting

Results 1 to 3 of 3

Thread: Use Find in a loop

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Use Find in a loop

    I have a column that i would like to see if there is a duplicate on another sheet, before it runs a macro.

    So i would need it to test any values <> "" , in column "A:A" on Sheet1, and find if there is a match on Sheet2 Column "A:A"

    If one matches then

    If found

    MsgBox("There is a Match")

    else

    next

    I have the following code that works in a Userform but it only the value of a TextBox in the Userform, i need it to look at all values in a column now

    Set Found = wd.Range("A5:A60000").Find(UserForm1.TxtSerial.Value, LookIn:=xlValues)
            
                    If Found Is Nothing Then
                
                    .Cells(RowNext1, 1) = UserForm2.TxtDate.Value
                    .Cells(RowNext1, 2) = UserForm2.TxtSerial.Value
                    .Cells(RowNext1, 3) = Environ("Username")
    
    
                    Else
                    
                    FDate = Found.Offset(0, -2).Value
                        
                       If MsgBox("Serial Number Found on " & FDate & Chr(10) & _
                       "Do You Still Want To Process?", vbYesNo) = vbYes Then
    
    
                    .Cells(RowNext1, 1) = UserForm2.TxtDate.Value
                    .Cells(RowNext1, 2) = UserForm2.TxtSerial.Value
                    .Cells(RowNext1, 3) = Environ("Username")
    
    
    
    End If

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    in the case of large data sets and assuming macro finds 10.000 matches in range A5:A60000, do you want msgbox will pop up 10.000 times?


    Sub vbax_54279_Compare_Cells_In_Two_Cols_Different_WS()
         
        Dim i As Long, Found As Range
         
        For i = 2 To Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
            On Error Resume Next
            Set Found = Worksheets("Sheet2").Range("A5:A60000").Find(Worksheets("Sheet1").Range("A" & i), LookIn:=xlValues)
            On Error GoTo 0
            If Found Is Nothing Then
                'codes when no match
            Else
                'codes when match
            End If
        Next i
         
    End Sub
    Last edited by mancubus; 11-16-2015 at 01:17 AM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    You bring up a good point, but in most cases it will only find one or two, since the data it will look for will not exceed 3 values in Sheet1.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •