I've been working with some vba code to copy multiple worksheets from a variety of files into different workbooks. Whilst the code has worked well it does have some limitations. In particular when the base file contains hidden sheets.
Basically when the "base" file contains a hidden sheet, lets say Extract sheet 2 is hidden. The following code will pull an error at the line Sheets("sheet2").Select, when trying to copy sheet 2.
From my understanding this is because it initially recognises there is a sheet in the Instr code, even though it is hidden. Then down a few lines it is trying to then select the sheet, which it fails to do because it is hidden.
Any help would be much appreciated, I only want to run the Instr code on active not hidden sheets and ignore all hidden sheets. Example code as follows
Dim book1 as workbook
Dim book2 as workbook
Dim ws as worksheet
book1.Activate
For Each ws In ActiveWorkbook.Worksheets
If InStr(1, ws.Name, "sheet2") Then
book2.Activate
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "X2"
book1.Activate
Sheets("sheet2").Select
Set ws = ActiveSheet
If ActiveSheet.AutoFilterMode Or ActiveSheet.FilterMode Then
ActiveSheet.AutoFilterMode = False
End If
Cells.Select
Application.CutCopyMode = False
Selection.Copy
book2.Activate
Sheets("x").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.Columns.AutoFit
End If
Exit For
Next`