Help with VBA Find function

Davidharter90

New Member
Joined
Jun 27, 2013
Messages
6
So a little background. I have a worksheet with about 600k rows of data (oil and gas wells). Each well has its own unique 14 digit identifier in column A and once a year i update our database with the new wells. Before copying over the new wells into the database, the code is suppose to first find and delete the wells already in the database which is where my problem arises.

I dim all my workbooks and worksheets outside of the sub routine btw
Here's the code:
Code:
Sub chkduplicates()
Dim chkfind As Range
Set wbook = ThisWorkbook
Set chsheet = wbook.Worksheets("Check")
Set ncsheet = wbook.Worksheets("NewCasing")
Set fsheet = wbook.Worksheets("Format")


lrownc = Range("A1").End(xlDown).Row
lcolnc = Range("A1").End(xlToRight).Column
chsheet.Select
lrowch = Range("A1").End(xlDown).Row
Range(Cells(3, 2), Cells(lrowch, 2)).Clear
chsheet.Range(Cells(3, 1), Cells(lrowch, 1)).Copy
chsheet.Cells(3, 2).Select
ActiveSheet.Paste
Cells(3, 2).Select
Application.CutCopyMode = False
ncsheet.Select


i = 0
j = 0
k = 0
10: Cells(2 + j, 1).Select
For i = k To lrowch
    chkval = chsheet.Cells(3 + i, 2).Value
    Set chkfind = Cells.Find(What:=chkval, After:=Cells(1, 1), LookIn:=xlFormulas _
        , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
'Maybe it's running to fast...I don't know so lets try a wait...
    Application.Wait Now + #12:00:02 AM#
    If chkfind Is Nothing Then
        chsheet.Cells(3 + i, 2).Clear
        GoTo 11
    Else
        delrow = chkfind.Row + 1
        chkval2 = chkfind.Value
    End If
    If chkval = chkval2 Then
        Do Until chkval <> chkval2
            chkval2 = Cells(delrow - 1 + j, 1).Value
            j = j + 1
        Loop
        delrow2 = Cells(delrow - 1 + j, 1).Row - 2
        Range(Cells(delrow - 1, 1), Cells(delrow2, lcolnc)).Select
        If delrow <= 2 Or delrow2 <= 2 Then
            k = i
            j = 0
            lrownc = Range("A1").End(xlDown).Row
            GoTo 10
        End If
        Selection.Delete
        lrownc = Range("A1").End(xlDown).Row
        chsheet.Cells(3 + i, 2).Clear
        delrow = ""
        delrow2 = ""
        Set chkfind = Nothing
    End If
11: j = 0
    Cells(2 + j, 1).Select
    chkval2 = ""
Next i
Cells(1, 1).Select
chsheet.Select
Range(Cells(3, 2), Cells(lrowch, 2)).Clear
ncsheet.Select
End Sub

When i F8 or F5 (with stops) the code works perfectly fine and does exactly what i want it to do.
1) Grabs the existing well number from the other worksheet tab
2) Finds the well number in the database tab
3) Deletes the well from the database tab
4) Clears the well from the other worksheet tab and moves on to the next well number

When I just run the code as normal, the Find function stops working and returns chkfind=nothing even though the well number exists. I even it did it manually by ctrl+F and that finds the value just fine so I know the well is there but for some reason the VBA find wont return it. I can't figure this out so I'm hoping someone on here can.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If you only need to find the row the well is in why not use Application.Match?

Something like this perhaps.
Code:
Sub chkduplicates()
Dim Res As Variant

    Set wbook = ThisWorkbook
    Set chsheet = wbook.Worksheets("Check")
    Set ncsheet = wbook.Worksheets("NewCasing")
    Set fsheet = wbook.Worksheets("Format")


    lrownc = Range("A1").End(xlDown).Row
    lcolnc = Range("A1").End(xlToRight).Column
    chsheet.Select
    lrowch = Range("A1").End(xlDown).Row
    Range(Cells(3, 2), Cells(lrowch, 2)).Clear
    chsheet.Range(Cells(3, 1), Cells(lrowch, 1)).Copy
    chsheet.Cells(3, 2).Select
    ActiveSheet.Paste
    Cells(3, 2).Select
    Application.CutCopyMode = False
    ncsheet.Select


    i = 0
    j = 0
    k = 0
10:     Cells(2 + j, 1).Select
    For i = k To lrowch
        chkval = chsheet.Cells(3 + i, 2).Value
        
        Res = Application.Match(chkval, ncsheet.Columns(1), 0)
        
        If IsError(Res) Then
            chsheet.Cells(3 + i, 2).Clear
            GoTo 11
        Else
            delrow = Res + 1
            chkval2 = ncsheet.Range("A" & Res)
        End If
        
        If chkval = chkval2 Then
            Do Until chkval <> chkval2
                chkval2 = Cells(delrow - 1 + j, 1).Value
                j = j + 1
            Loop
            delrow2 = Cells(delrow - 1 + j, 1).Row - 2
            Range(Cells(delrow - 1, 1), Cells(delrow2, lcolnc)).Select
            If delrow <= 2 Or delrow2 <= 2 Then
                k = i
                j = 0
                lrownc = Range("A1").End(xlDown).Row
                GoTo 10
            End If
            Selection.Delete
            lrownc = Range("A1").End(xlDown).Row
            chsheet.Cells(3 + i, 2).Clear
            delrow = ""
            delrow2 = ""
            Set chkfind = Nothing
        End If
11:         j = 0
        Cells(2 + j, 1).Select
        chkval2 = ""
    Next i
    
    Application.Goto ncsheet.Cells(1, 1)
    
    With chsheet
        .Range(.Cells(3, 2), .Cells(lrowch, 2)).Clear
    End With
    
    ncsheet.Select
    
End Sub
 
Upvote 0
Can you post an example of a chkval that is present but can't be found? Is it a string, a number or what ....? Is it being returned by a formula or is it a constant?
 
Upvote 0
Can you post an example of a chkval that is present but can't be found? Is it a string, a number or what ....? Is it being returned by a formula or is it a constant?

An example of the chkval is 35029212990000 and its formatted as a number and no formulas
 
Upvote 0
Norie,
I don't know why the find wasn't working but your solution solved my problem. Thank you! New code:

Code:
Sub chkduplicates()
Dim Res As Variant
Set wbook = ThisWorkbook
Set chsheet = wbook.Worksheets("Check")
Set ncsheet = wbook.Worksheets("NewCasing")
Set fsheet = wbook.Worksheets("Format")


lrownc = Range("A1").End(xlDown).Row
lcolnc = Range("A1").End(xlToRight).Column
chsheet.Select
lrowch = Range("A1").End(xlDown).Row
Range(Cells(3, 2), Cells(lrowch, 2)).Clear
chsheet.Range(Cells(3, 1), Cells(lrowch, 1)).Copy
chsheet.Cells(3, 2).Select
ActiveSheet.Paste
Cells(3, 2).Select
Application.CutCopyMode = False
ncsheet.Select


i = 0
j = 0
k = 0
10: Cells(2 + j, 1).Select
For i = k To lrowch
    chkval = chsheet.Cells(3 + i, 2).Value
    Res = Application.Match(chkval, ncsheet.Columns(1), 0)
    If IsError(Res) Then
        chsheet.Cells(3 + i, 2).Clear
    Else
        delrow = Res + 1
        chkval2 = ncsheet.Range("A" & Res).Value
    End If
    If chkval = chkval2 Then
        Do Until chkval <> chkval2
            chkval2 = Cells(delrow - 1 + j, 1).Value
            j = j + 1
        Loop
        delrow2 = Cells(delrow - 1 + j, 1).Row - 2
        Range(Cells(delrow - 1, 1), Cells(delrow2, lcolnc)).Select
        If delrow <= 2 Or delrow2 <= 2 Then
            k = i
            j = 0
            lrownc = Range("A1").End(xlDown).Row
            GoTo 10
        End If
        Selection.Delete
        lrownc = Range("A1").End(xlDown).Row
        chsheet.Cells(3 + i, 2).Clear
        delrow = ""
        delrow2 = ""
    End If
    j = 0
    Cells(2 + j, 1).Select
    chkval2 = ""
Next i
Cells(1, 1).Select
chsheet.Select
Range(Cells(3, 2), Cells(lrowch, 2)).Clear
ncsheet.Select
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top