Sheet is not found/available Help

fhan

New Member
Joined
Mar 28, 2002
Messages
11
I am try to write a macro to open up several files and look for a particular sheet. If the sheet is found to copy it over another workbook. However, my dilemma comes when that sheet is not found/available. Is there a way to write into the macro to move to the next workbook if the sheet is not found?

This is what I got so far. Any suggestion is appreciated.

Windows("Book2").Activate
Sheets("Sheet2").Select
Sheets("Sheet2").Copy After:=Workbooks("Book1").Sheets(3)

Windows("Book3").Activate
Sheets("Sheet2").Select
Sheets("Sheet2").Copy After:=Workbooks("Book1").Sheets(4)
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this...



Sub transfer_sheets()


find_sheets_called = "Sheet2"
copy_sheets_to = "Book1"

For wn = 1 To Workbooks.Count
If Application.Workbooks(wn).Name <> copy_sheets_to Then
For sn = 1 To Workbooks(wn).Sheets.Count
If Workbooks(wn).Sheets(sn).Name = find_sheets_called Then
Workbooks(wn).Sheets(sn).Copy Before:=Workbooks(copy_sheets_to).Sheets(1)
End If
Next
End If
Next

End Sub
 
Upvote 0
Hi

This may get you on your way. It makes use of the On Error Resume Next Statement, which means the code will continue if it encounters a non existing sheet.

Dim wBook As Workbook

On Error Resume Next
For Each wBook In Application.Workbooks
wBook.Sheets("Sheet2").Copy After:=Workbooks("Book1.xls").Sheets(3)
Next wBook
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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