PDA

View Full Version : Copy sheets from file to another but Not Copying those that already exist in 2nd file



TGarrett
02-16-2016, 11:23 AM
Hi all,

What I am trying to do is copy any worksheets from one file (source) to another workbook (target) which don't already exist in the target file. Is there a way to loop through and compare each of the worksheet names and only copy the new ones while ignoring those that are already in the target? The sheets are named in sequential order (JD001, JD002, JD003...) and the file is updated daily with new sheets. I need to perform this function each day to copy my office's schedule and reformat it for printing. I had it set up to always go to the last worksheet in the source file and copy it to the target but then I learned that two or three new schedules are sometimes dropped at the same time to cover holidays and weekends. This is where the looping will come into play. It may take a little bit longer to run but I need to make sure all new sheets are copied and reformatted each time I run the macro. Maybe an array would work to compare the two workbooks and select the sheets to copy? Any help would be greatly appreciated. Thanks.

Tom

SamT
02-16-2016, 12:46 PM
Sub Example_Usage()
If Not SheetExists(Book1.Sheets("Sheet1").Name) Then _
Book1.Sheets("Sheet1").Copy ' to ThisWorkbook
End Sub


Function SheetExists(ShtName As String, Optional ByVal WkBk As Workbook) As Boolean
'By Chip Pearson
On Error Resume Next
If WkBk Is Nothing Then Set WkBk = ThisWorkbook
SheetExists = CBool(Len(WkBk.Sheets(ShtName).Name))
End Function