error handler only catches once

said76

New Member
Joined
Sep 7, 2011
Messages
5
Hi,

I have the following codes that would do copy and paste between one worksheet and another sheet in the same workbook. However, the problem I am having is catching the error. The error handler seems to work only once. Is there a way to reset it or perhaps I have done it the wrong way.

Code:
For counter = 1 To WorkSheets.Count
   
    Worksheets(counter).Activate
    
    MyCol = 1

    MyRowIndex = 1
    
    'On Error GoTo Errhandler
    Do While Worksheets(counter).Range("A" & MyRowIndex) <> "" 
        On Error GoTo Errhandler
        'On Error Resume Next

        If Worksheets(counter).Range("J" & MyRowIndex) = "Cap" Then
  
            Worksheets(counter).Range("J" & MyRowIndex).EntireRow.Copy
            
            Sheets("Sheet3").Select
            Range("A" & MyCol).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
         
            Worksheets("Sheet3").Range("O" & MyCol) = Worksheets(counter).Name
            MyCol = MyCol + 1      
        End If
        
sdfs:
        MyRowIndex = MyRowIndex + 1
    
    Loop

Errhandler:
    If Err.Number = 13 Then
        GoTo sdfs
    'Else
        'Resume Next
    End If

Next i

What this code does is to go through each of the worksheets and in each of the sheets, do the loop in search of a particular value in column J. The reason I need to apply the error handler here is in some of the sheets, there are #N/A value. So I need to be able to skip that and continue to loop through down the column.

Any help would be greatly appreciated.

Thank you
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You cannot use Goto to exit an error handler - you have to use Resume, otherwise all the code that runs afterwards is treated as being part of the error handler routine, and any error that occurs in an error handler causes a run time error.

However, here it would be simpler to add an IsError test or check the cell's Text property rather than Value. You can also use CStr to convert the Value to text and then you won't get a type mismatch error, even if the cell contains an error value.
 
Upvote 0

Forum statistics

Threads
1,224,562
Messages
6,179,526
Members
452,923
Latest member
JackiG

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