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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,214,402
Messages
6,119,304
Members
448,886
Latest member
GBCTeacher

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