Error handling in For Each Loop

Gahlilama

New Member
Joined
Jun 21, 2011
Messages
10
Hello,

I have a short macro that loops thru a range, and for each row:
1. grabs a value from the row
2. looks for that value in another sheet
3. copies a range once the value is found
4. returns to the first sheet and pastes the copied data.

The loop works 100% if every value is found, but I naturally get an error if the value it's looking for is not found. I don't know how to get the loop to bypass the error and go to the next row to search for the next value. I'm am somewhat familiar with error handling and I can get this work in a non-loop version but cannot get past the error in a loop.

Any help would be much appreciated.


VBA Code:
Sub Find_Categories()
'
    Application.ScreenUpdating = False
'
    Range("F1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, 5).Range("A1").Select
    Range(Selection, "K2").Select
    For Each Cell In Selection
        If Cell.Value = 0 Then
'
        ThisWorkbook.Activate
        ActiveSheet.Select
  
        Dim Title1 As String
        Title1 = Range("E" & ActiveCell.Row)
        Workbooks("Reference For Billing.xlsm").Activate
        Sheets("New Releases").Select
        Range("A1").Select
'the error occurs at this line when it can't find the value
        Cells.Find(What:=Title1, After:=ActiveCell, LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        Range("D" & ActiveCell.Row & ":" & "E" & ActiveCell.Row).Copy
        ThisWorkbook.Activate
        Range("L" & ActiveCell.Row).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        ActiveCell.Offset(1, 0).Range("A1").Select
'
    Else: ActiveCell.Offset(1, 0).Range("A1").Select
    End If
Next Cell
'
   Application.ScreenUpdating = True
'
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Setting error handler to 0 is something I avoid, but that is the suggestion half way down the page here

I intend to play around with this, but later.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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