VBA: Error Handling in For Each loop - On Error Resume Next ws

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
149
Good morning,

The code below copy / pastes values from one workbook to another for all identical sheet names. However, when the active workbook contains a sheet not found in the "past wb", an error occurs. How do you tell the code to skip that sheet and continue on to the next (ie "On Error Resume Next ws")??

Thank you!

Code:
For Each ws In ActiveWorkbook.Worksheets
 
        Sheets(ws.Name).Activate
        Range("F30").Select
        Workbooks(pastWBname).Sheets(ws.Name).Activate
                For i = 1 To 7
                Selection.Copy
                Selection.Offset(, 2).Select
                Windows(myWB.Name).Activate
                ActiveCell.PasteSpecial xlPasteValues
                ActiveCell.Offset(, 2).Select
                Windows(pastWBname).Activate

            Next i


Next ws
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try
Code:
For Each Ws In ActiveWorkbook.Worksheets
   [COLOR=#ff0000]If ShtExists(Ws.Name, Workbooks(pastWbname)) Then[/COLOR]
      Sheets(Ws.Name).Activate
      Range("F30").Select
      Workbooks(pastWbname).Sheets(Ws.Name).Activate
      For i = 1 To 7
         Selection.Copy
         Selection.Offset(, 2).Select
         Windows(MyWB.Name).Activate
         ActiveCell.PasteSpecial xlPasteValues
         ActiveCell.Offset(, 2).Select
         Windows(pastWbname).Activate
      Next i
  [COLOR=#ff0000] End If[/COLOR]
Next Ws
And
Code:
Public Function ShtExists(ShtName As String, Optional wbk As Workbook) As Boolean
    If wbk Is Nothing Then Set wbk = ActiveWorkbook
    On Error Resume Next
    ShtExists = (LCase(wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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