Copy Worksheet to Workbook from mutliple files

kaffal

Board Regular
Joined
Mar 7, 2009
Messages
68
Hi all, I found some code to copy the worksheet from one workbook.

Currently I am able to copy the worksheet using hardcoded path from cell value,D3 and filename from cell value, D4.

However there is more than one worksheet to copy over in the directory.
I would like to program in a way that it will locate all the files with the filename contain cell value,D4 and do the copy of worksheet over, into a newly created worksheet named with the cell value, A1.

For example, the cell value,D4 is 06032009. In the directory, there are 10 files with 3 files contain the cell value (
06032009_prod1.xls, 06032009_prod2.xls, 06032009_prod3.xls).

The macro will then create 3 new tabs with the content being copied over.


Code:
Sub CopyWorksheet()

    Sheets("Menu").Select
    PathName = Range("D3").Value
    FileName = Range("D4").Value

    ControlFile = ActiveWorkbook.Name
    Workbooks.Open FileName:=PathName & FileName
    TabName = Range("A1").Value
    ActiveSheet.Name = TabName
    Sheets(TabName).Copy After:=Workbooks(ControlFile).Sheets(1)
    Windows(FileName).Activate
    ActiveWorkbook.Close SaveChanges:=False
    Windows(ControlFile).Activate
    Sheets("Menu").Select
    Range("D8").Select
    ActiveCell.Value = "Completed"
    Range("D9").Select
End Sub

 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Slightly different from what you ask. However.

This code will loop through the files in the specified folder, rename the sheet to the range starting in A2 ( the way you describe it would always be the same name? ). Or perhaps you mean using the sheetname?

Excel Workbook
ABCD
1TabnameStatus
2123Fname
3124c:\temp\
4125
5126
6127
7128
8129
9130
10131
11132
12133
13134
14135
15136
16137
17138
18139
19140
20141
menu


Code:
Sub RunCodeOnAllXLSFiles()

Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next

Set wbCodeBook = ThisWorkbook
i = 2

fpath = Sheets("menu").Range("D3")

    With Application.FileSearch
        .NewSearch
         'Change path to suit
        .LookIn = fpath
        .FileType = msoFileTypeExcelWorkbooks
        '.Filename = "Book*.xls"
    

            If .Execute > 0 Then 'Workbooks in folder

              For lCount = 1 To .FoundFiles.Count 'Loop through all.
                 'Open Workbook x and Set a Workbook variable to it
                 Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)


                 'DO YOUR CODE HERE
                      
                                  
                  tabname = wbCodeBook.Sheets("Menu").Cells(i, 1).Value
                  ActiveSheet.Name = tabname
                  ActiveSheet.Copy After:=wbCodeBook.Sheets(1)
                  i = i + 1
                                   
                  wbCodeBook.Sheets("Menu").Cells(i, 2).Value = "Completed"
                  wbResults.Close SaveChanges:=True
                 
                 Next lCount

            End If

    End With

    On Error GoTo 0


    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True



End Sub
 
Upvote 0
hi , It worked . However there remain one more problem.
Code:
        .FileName = "06032009*.xls"

Instead of changing inside the code itself, to define the search of "06032009".
Can I assign to a specific cell value,D4?

For example, If I enter 10032009 in cell,D4. It will find all the files name contain 10032009 instead of 06032009.
 
Upvote 0
.filename = range("d4") & "*.xls"

note application.filesearch is discontinued, vba Dir will also return the same files, with slightly different code
 
Upvote 0
Great it works, sorry I have one more problem.

I added one condition to satisty before it will be copied over to the workbook.
However I got one more problem, I noticed that macro will open up all files contain my search criteria. If the condition of cell value,B3 is not empty, it will copied over and the file is closed.
If the cell value is empty, the worksheet wun be copied over and it remain opened. How to close all up those open files even though the condition is not satisfied. How can i change the code to do that ?

Another off topic noob question, I understand that worksheets need to open in the macro, but i can see that files are opening and closing in the toolbars during the extraction. Is there any way to stop the opening and closing of files not seen by the users?

Code:
For lCount = 1 To .FoundFiles.Count 'Loop through all.
                 'Open Workbook x and Set a Workbook variable to it
                 Set wbResults = Workbooks.Open(FileName:=.FoundFiles(lCount), UpdateLinks:=0)


                 'DO YOUR CODE HERE
                  TabName = Range("A1").Value
                  EmptyCell = Range("B3").Value
                  If EmptyCell <> "" Then
                  ActiveSheet.Name = TabName
                                  
                  Sheets(TabName).Copy After:=wbCodeBook.Sheets(1)
                  i = i + 1
                                   
                  wbCodeBook.Sheets("Menu").Cells(i, 2).Value = "Completed"
                  
                  wbResults.Close SaveChanges:=True
                 End If
                 Next lCount
 
Upvote 0
move the wbresults.close line below the end if
if you don't want to save the file use a variable for savechanges, set to false when opening each file, then set to true within the if statement

for the display problem, you can try
application.screenupdating = false before the loop, reset to true when loop finishes
alternatively you can set the whole application to visible = false
 
Upvote 0
move the wbresults.close line below the end if
if you don't want to save the file use a variable for savechanges, set to false when opening each file, then set to true within the if statement

for the display problem, you can try
application.screenupdating = false before the loop, reset to true when loop finishes
alternatively you can set the whole application to visible = false

For the close file, it works.

However for the display problem, it does not works.
Any other advise ?
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

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