Dim wkbkdestination As Workbook
Dim destsheet As Worksheet
For Each ThisWorkSheet In wkbkorigin.Worksheets
'this throws subscript out of range if there is not a sheet in the destination
'workbook that has the same name as the current sheet in the origin workbook.
Set destsheet = wkbkdestination.Worksheets(ThisWorkSheet.Name)
Next
Basically I loop through all sheets in the origin workbook then set destsheet
in the destination workbook to the sheet with the same name as the currently iterated one in the origin workbook.
How can I test if that sheet exists? Something like:
If wkbkdestination.Worksheets(ThisWorkSheet.Name) Then
If you are a fan of
WorksheetFunction.
or you work from a non-English country with a non-English Excel this is a good solution, that works:Or in a function like this:
I did another thing: delete a sheet only if it's exists - not to get an error if it doesn't:
As checking for members of a collection is a general problem, here is an abstracted version of Tim's answer:
This function can be used with any collection like object (
Shapes
,Range
,Names
,Workbooks
, etc.).To check for the existence of a sheet, use
If Contains(Sheets, "SheetName") ...
Corrected: Without error-handling:
Why not just use a small loop to determine whether the named worksheet exists? Say if you were looking for a Worksheet named "Sheet1" in the currently opened workbook.