Copy data from closed workbooks ... Continued

I recently asked

to which sktneer provided me with the below Macro. However I now need a variation on this which is:

this piece of code determines the No of Tabs : If Slr > 6 And cnt <= 14 Then


how easy difficult would it be to change it so that  instead of doing it that way in the Code I name the Sheets to exclude.

The reason I ask is, during testing the users added two new sheets they wanted to include. because the code determined the number of sheets it did not pick up the extra 2 sheets. can the code be changed to say 'Exclude Sheetx, Sheety, Sheetz, etc, etc.

Sub CopyDataFromWorkbooks()
Dim Swb As Workbook, Dwb As Workbook
Dim ws As Worksheet, Dws As Worksheet
Dim fso As Scripting.FileSystemObject
Dim fil As Scripting.File
Dim sFolderPath As String
Dim SourceFolder As Scripting.Folder
Dim Dlr As Long, Slr As Long, cnt As Long

Application.ScreenUpdating = False

sFolderPath = "C:\Jagwarman\My Files"   'Source Folder Path
Set Dwb = ThisWorkbook
Set Dws = Dwb.Sheets("Sheet1")             'Destination Sheet on the Master Workbook where data will be copied

Dlr = Dws.Cells(Rows.Count, 1).End(xlUp).Row
If Dlr > 6 Then Dws.Range("A7:S" & Dlr).Clear

Set fso = New Scripting.FileSystemObject

If Not fso.FolderExists(sFolderPath) Then
    MsgBox "Source Folder does not exists."
    Exit Sub
End If

Set SourceFolder = fso.GetFolder(sFolderPath)

For Each fil In SourceFolder.Files
    If Left(fso.GetExtensionName(fil.Path), 2) = "xl" And fil.Name <> Dwb.Name And Left(fil.Name, 1) <> "~" Then
        Application.StatusBar = "Processing file " & fil.Name & " of folder " & sFolderPath & "."
        Workbooks.Open fil.Path
        Set Swb = ActiveWorkbook
        For Each ws In Swb.Worksheets
            Slr = ws.Cells(Rows.Count, 1).End(xlUp).Row
            cnt = cnt + 1
            If Slr > 6 And cnt <= 14 Then
                Dlr = Dws.Cells(Rows.Count, 1).End(xlUp).Row + 1
                If Dlr <= 6 Then Dlr = 7
                ws.Range("A7:S" & Slr).Copy Dws.Range("A" & Dlr)
                Application.CutCopyMode = 0
            End If
        Next ws
        Swb.Close False
    End If
    cnt = 0
Next fil
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox "Task Complete.", vbInformation
End Sub

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
I think the following change will resolve your issue...

For Each ws In Swb.Worksheets
    If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" Then
        Slr = ws.Cells(Rows.Count, 1).End(xlUp).Row
        If Slr > 6 Then
            Dlr = Dws.Cells(Rows.Count, 1).End(xlUp).Row + 1
            If Dlr <= 6 Then Dlr = 7
            ws.Range("A7:S" & Slr).Copy
            Dws.Range("A" & Dlr).PasteSpecial xlPasteValues
            Application.CutCopyMode = 0
        End If
    End If
Next ws

Open in new window

The line If Slr > 6 Then checks if there is data below row6 and then only it will copy the data from the sheet. If you don't need this condition, remove this line and one End If from the above code.

The line of code If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" Then makes sure that the code will copy the data from the sheet if the sheet's name is not Sheet1 or Sheet2. You can change the Sheets Name as per your requirement.

Does this resolve your issue?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JagwarmanAuthor Commented:
Had to leave work early will review this over the weekend.

Much appreciated
JagwarmanAuthor Commented:
sktneer, perfect. Many thanks
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome. Glad to help. :)
JagwarmanAuthor Commented:
just posted new question
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.