Copying from only select worksheets within multiple workbooks

liketohear

New Member
Joined
Sep 16, 2019
Messages
2
Hi,

I've got some code that is looping through multiple workbooks (with multiple worksheets) to copy data however it's pulling ALL the worksheets within these workbooks and i'd like to limit it the copying to specific ones. Is there a way to list the worksheets that i only want the code to copy from? (similar to SQL where you would use the IN ('XYZ','ABC') code).

Do Until Value = ""
If Value = "." Or Value = ".." Then
Else
If Right(Value, 3) = "xls" Or Right(Value, 4) = "xlsx" Or Right(Value, 4) = "xlsm" Then
On Error Resume Next
Workbooks.Open Filename:=myfolder & Value, Password:="zzzzzzzzzzzz"
If Err.Number > 0 Then
Else
On Error GoTo 0
For Each sht In ActiveWorkbook.Worksheets

'If sht.Range("D37:J52") <> "" Then
Lrow = WS.Range("A" & Rows.Count).End(xlUp).Row + 1
sht.Range("D37:J52").Copy Destination:=WS.Range("A" & Lrow)
'End If

Next sht

End If
Workbooks(Value).Close False
On Error GoTo 0
End If
End If
Value = Dir
Loop

Thanks!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You can adapt this to limit which sheets your code will act upon;
Code:
Sub OnlyActOnCertainSheetsInWb()
Dim CertainSheets As Variant, Sht As Worksheet
CertainSheets = Array("Sheet1", "Sheet2", "Sheet4") 'Change sheet names to suit
For Each Sht In Sheets
    For i = LBound(CertainSheets) To UBound(CertainSheets)
        If Sht.Name = CertainSheets(i) Then
            'do something to this sheet
        End If
    Next i
Next Sht


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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