error on exit for VBA

negi

Board Regular
Joined
Apr 16, 2009
Messages
87
Hi experts,
i m using this code but its not working when found error or no value. please help on this

i want if any cell found value then skip for next cell


Sub match()
Dim rng As Range, FinalResult As Variant, Table_Range As Range
Dim L1 As Range
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim cn_m As Range
Dim lastrow As Long
Dim i As Long


Set ws = Sheets("Data")
Set ws1 = Sheets("Sheet2")
Set cn_m = ws.Range("A:A")

lastrow = ws1.Cells(Rows.Count, "A").End(xlUp).Row

For i = 2 To lastrow

Set L1 = ws1.Range("A" & i)

On Error GoTo NM

If Application.WorksheetFunction.match(L1, cn_m, 0) > 0 Then

Exit For

Else
NM:

MsgBox "New Record"

End If
Next i
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
i want if any cell found value then skip for next cell
The Exit For you have in your code contradicts the statement above since it is applying the Exit For when the match > 0, which equals Found.

See if the below points you in the right direction. You will need to determine what action to take when found vs not found.

VBA Code:
Sub match_mod()
    Dim rng As Range, FinalResult As Variant, Table_Range As Range
    Dim L1 As Range
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim cn_m As Range
    Dim lastrow As Long
    Dim i As Long
    
    Set ws = Sheets("Data")
    Set ws1 = Sheets("Sheet2")
    Set cn_m = ws.Range("A:A")
    
    lastrow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To lastrow
    
        Set L1 = ws1.Range("A" & i)
        
        If Application.IfError(Application.match(L1, cn_m, 0), 0) > 0 Then
            MsgBox "Found: " & L1.Value
        Else
            MsgBox "New Record"
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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