Copying sheets from multiple workbooks into a master workbook

cw1

New Member
Joined
Mar 25, 2010
Messages
10
HI Guys i'm hoping one of you will be able to help me with the macro i'm trying to create as I have no idea what I am doing!

I have multiple workbooks all contained in one folder which all have a sheet titled "Summary".
I have another workbook which I would like to pull all the "summary" sheets into (if possible values and format) and change the name of the sheet to the contents of one of the cells in the sheet plus the word summary eg cell B2 = Widget therefore sheet name should be "WidgetSummary" So I will end up with the master file which already has 2 sheets in it plus a number of summary sheets all pulled from different workbooks which are stored in 1 folder.

I hope that makes sense, any help would be really appreciated.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi
Create a workbook called CW1.xls with the following codes and save it inside the folder of your interest. Run the macro
Code:
Sub CW1()
Dim z  As Long, e As Long
Dim f As String, b As String, c As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks("CW1.xls").Sheets("Sheet1").Cells(1, 1) = "=cell(""filename"")"
Workbooks("CW1.xls").Sheets("Sheet1").Cells(1, 2) = "=left(A1,find(""["",A1)-1)"
Workbooks("CW1.xls").Sheets("Sheet1").Cells(2, 1).Select
f = Dir(Workbooks("CW1.xls").Sheets("Sheet1").Cells(1, 2) & "*.xls")
    Do While Len(f) > 0
    ActiveCell.Formula = f
    ActiveCell.Offset(1, 0).Select
    f = Dir()
    Loop
z = Workbooks("CW1.xls").Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    For e = 2 To z
         b = Workbooks("CW1.xls").Sheets("Sheet1").Cells(e, 1)
         If b <> ActiveWorkbook.Name Then
        c = Mid(Left(b, Len(b) - 4), 1, 20) & " - " & "summary"
        Workbooks.Open Filename:=Workbooks("CW1.xls").Sheets("Sheet1").Cells(1, 2) & Workbooks("CW1.xls").Sheets("Sheet1").Cells(e, 1)
        Worksheets("Summary").UsedRange.Copy 'Change sheetname if it is incorrect
        ActiveWorkbook.Close False
        Sheets.Add.Name = c
        Sheets(c).Range("A1").PasteSpecial
        End If
    Next e
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
MsgBox "collating is complete."
End Sub
It lists all filenames in col A, opens each of them and copies the summary sheet to a new added sheet with filename and summary.
Ravi
 
Upvote 0
I think I must be missing something i've copied the following as I think it should do what i need but i can't get it to work

Sub RDB_Copy_Sheet()
Dim myFiles As Variant
Dim myCountOfFiles As Long
myCountOfFiles = Get_File_Names( _
MyPath:="Y:\Macro test for rolling forecast", _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)
If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If
Get_Sheet _
PasteAsValues:=True, _
SourceShName:="", _
SourceShIndex:=1, _
myReturnedFiles:=myFiles

End Sub

When i try running this i get the error Complie error : Sub or function not defined and it highlights myCountOfFiles = Get_File_Names( _


any ideas??
 
Upvote 0
You missed a setp:

Information

Use Alt F11 to open the VBA editor and you see that there are four modules in this workbook.
The code in the module named "Basic_Code_Module" will be used by every example in this workbook.
Note: you not have to change anything in this module.
 
Upvote 0
HI ravishankar,

Many thanks for that it seems to work I just have a couple of queries.

  1. I don't seem to be able to run it from the excel menu only from VB, do you know why this would be?
  2. Would it be possible to paste the formula as well as the values?
  3. This only seems to work if the workbook CW1 is completely empty, but I would like to have to worksheets permanently in the workbook CW1 would that be possible.
Thanks again for your help
 
Upvote 0
You missed a setp:

Information

Use Alt F11 to open the VBA editor and you see that there are four modules in this workbook.
The code in the module named "Basic_Code_Module" will be used by every example in this workbook.
Note: you not have to change anything in this module.


Hi Andrew,

Thanks for this - I've tried running the copy sheet macro which seems to do what I need but inconsistently. The first sheet it has copied it has copid values and format which is perfect and renamed the sheet. However it has not done this with all the sheets, some have copied with values but not format and some have not changed from the original sheet name.

Any ideas how i resolve this?

Thanks
 
Upvote 0
You missed a setp:

Information

Use Alt F11 to open the VBA editor and you see that there are four modules in this workbook.
The code in the module named "Basic_Code_Module" will be used by every example in this workbook.
Note: you not have to change anything in this module.

Hi Andrew,

Thanks for this - I've tried running the copy sheet macro which seems to do what I need but inconsistently. The first sheet it has copied it has copid values and format which is perfect and renamed the sheet. However it has not done this with all the sheets, some have copied with values but not format and some have not changed from the original sheet name.

Any ideas how i resolve this?

Thanks


Hi Andrew,

Sorry to be a pain It does seem to be working with the one exception of the naming of the sheets, some of the sheets are copying in and renaiming to the name of the file they copied from but others are keeping there original name do you know how i can resolve this. This is what I have so far

Then if there are files in the folder we call the macro "Get_Sheet"
'There are three arguments in this macro that we can change

'1) PasteAsValues = True to paste as values (recommend)
'2) SourceShName = sheet name, if "" it will use the SourceShIndex
'3) SourceShIndex = to avoid problems with different sheet names use the index (1 is the first worksheet)
' Do not change myReturnedFiles:=myFiles

Sub RDB_Copy_Sheet()
Dim myFiles As Variant
Dim myCountOfFiles As Long
myCountOfFiles = Get_File_Names( _
MyPath:="Y:\Macro test for rolling forecast", _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)
If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If
Get_Sheet _
PasteAsValues:=True, _
SourceShName:="", _
SourceShIndex:=2, _
myReturnedFiles:=myFiles
End Sub

' Note: You not have to change the macro below, you only
' edit and run the RDB_Copy_Sheet above.
Sub Get_Sheet(PasteAsValues As Boolean, SourceShName As String, _
SourceShIndex As Integer, myReturnedFiles As Variant)
Dim mybook As Workbook, BaseWks As Worksheet
Dim CalcMode As Long
Dim SourceSh As Variant
Dim sh As Worksheet
Dim I As Long
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
On Error GoTo ExitTheSub
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)

'Check if we use a named sheet or the index
If SourceShName = "" Then
SourceSh = SourceShIndex
Else
SourceSh = SourceShName
End If
'Loop through all files in the array(myFiles)
For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myReturnedFiles(I))
On Error GoTo 0
If Not mybook Is Nothing Then
'Set sh and check if it is a valid
On Error Resume Next
Set sh = mybook.Sheets(SourceSh)
If Err.Number > 0 Then
Err.Clear
Set sh = Nothing
End If
On Error GoTo 0
If Not sh Is Nothing Then
sh.Copy after:=BaseWks.Parent.Sheets(BaseWks.Parent.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = mybook.Name
On Error GoTo 0
If PasteAsValues = True Then
With ActiveSheet.UsedRange
.Value = .Value
End With
End If
End If
'Close the workbook without saving
mybook.Close savechanges:=False
End If
'Open the next workbook
Next I
' delete the first sheet in the workbook
Application.DisplayAlerts = False
On Error Resume Next
BaseWks.Delete
On Error GoTo 0
Application.DisplayAlerts = True
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub


Thanks again for any assistance you can offer
 
Upvote 0
The code changes the name of the sheet to the name of its workbook here:

Code:
On Error Resume Next
ActiveSheet.Name = mybook.Name
On Error GoTo 0

But it retains the original sheet name if there is more than one workbook with the same name (on second and subsequent workbooks with that name). You can't have more than one worksheet with the same name in a workbook.
 
Upvote 0
HI Andrew,

All the workbooks have the same name format bud different names for example they would be called Contractx finance Workbook.xls and ContractY finance workbook.xls etc. As no contracts have the same name no workbooks have the same name either but do have the same name format.
However it is pulling through the workbook name for some but the sheet name for others and as the sheet in each workbook is called summary it is bringing these in as summary1, summary2 etc.

Any ideas?

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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