My code is to merge multiple sheet from a folder. I achieved the first requirement which merge all the sheet1 of target workbooks. But now, i want to merge the 4th sheet of the target workbooks. before that i need to check whether the sheet is exist or not. if exist the code should merge the 4th sheets. This one also i managed to achieved. However if the the 4th sheet not exist the code should do nothing. This part im still stuck. below are the code.
Set shtDest = ActiveWorkbook.Sheets("MS2")
Filename = Dir(path & "\*.xls", vbNormal)
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
If Not Filename = ThisWB Then
Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "PID2" Then
Wkb.Sheets(4).Activate
Set CopyRng = Wkb.Sheets(4).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row)
CopyRng.Copy Dest
Wkb.Close False
"ElseIf Worksheets(i).Name <> "PID2" Then"
"Wkb.Close False"
"Exit Sub"
End If
Next i
End If
Filename = Dir()
Loop
Wkb.Sheets(4).Activateshould change toWorksheets(i).Activate