Hi,
I have over 400 workbooks that are exactly the same layout but contain different data in each one. I need to combine the data from all of these workbooks into one master spreadsheet.
1. The macro needs to pull the data from specific cells that are the same in each of the 400 workbooks, and paste/transpose them into separate rows on the master spreadsheet.
2. Every sheet is contained within subfolders in the folder "C:\Users\ta3991\Documents\Patch-clamp" on my PC.
3. Every excel sheet that I want to pull data from contains a sheet entitled "Summary data (all cells)".
4. The cells that need to be copied from each of the 400 workbooks are A1, B2:D2, B3:D3, B6:D6
5. The cells should be pasted so all the information from one column in one workbook is a row on the master sheet. I.e.
Workbook 1 cell A1 into P1, B2:D2 into Q2:Q4, B3:D3 into R2:R4, B6:D6 into S2:S4. Workbook 2 cell A1 into P1, B2:D2 into Q5:Q7, B3:D3 into R5:R7, B6:D6 into S5:S7
Etc. (so that cells from columns B-D from the sheet "Summary data (all cells)" from every workbook within "C:\Users\ta3991\Documents\Patch-clamp" is shown paste-transposed as its own row in the master workbook).
6. If its possible, I would also like the name of each of the 400 files to appear in column O on the master workbook alongside the data that has come from that file. I have no idea how to do this bit so I have not included it in my attempt so far.
Any help at all would be greatly appreciated!
Macro attempt so far:
Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim fileName As String
Dim ws As Worksheet
Dim counter As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Set summarysheet to activeworkbook/activesheet where the macro runs
Set SummarySheet = ActiveWorkbook.ActiveSheet
'Select Folder
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = "C:\Users\Natasha\Documents\PhD\Patch-clamp"
If .Show = -1 Then
FolderPath = .SelectedItems(1) & ""
Else
Exit Sub 'User Canceled
End If
End With
' Call Dir the first time, pointing it to all Excel files in the folder path.
fileName = Dir(FolderPath & "*.xlsx*")
Application.ScreenUpdating = False
' Loop until Dir returns an empty string.
Do While fileName <> ""
' Open a workbook in the folder
With Workbooks.Open(FolderPath & fileName)
' Set the source worksheet
Set ws = Nothing
On Error Resume Next
Set ws = .Sheets("Summary data (all cells)")
On Error GoTo 0
If Not ws Is Nothing Then
NextRow = SummarySheet.Range("A" & Rows.Count).End(xlUp).Row + 1
' Copy over the values from the source to the destination next row.
ws.Range("A1").Copy
SummarySheet.Range("P1").PasteSpecial Paste:=xlPasteValues, Transpose:=False
ws.Range("B2:D2").Copy
SummarySheet.Range("Q2" & NextRow).PasteSpecial Paste:=xlPasteValues, Transpose:=True
ws.Range("B3:D3").Copy
SummarySheet.Range("R2" & NextRow).PasteSpecial Paste:=xlPasteValues, Transpose:=True
ws.Range("B6:D6").Copy
SummarySheet.Range("S2" & NextRow).PasteSpecial Paste:=xlPasteValues, Transpose:=True
counter = counter + 1
End If
' Close the source workbook without saving changes.
.Close SaveChanges:=False
End With
' Use Dir to get the next file name.
fileName = Dir()
Loop
Application.ScreenUpdating = True
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
MsgBox counter & " workbooks consolidated. ", , "Consolidation Complete"
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I have over 400 workbooks that are exactly the same layout but contain different data in each one. I need to combine the data from all of these workbooks into one master spreadsheet.
1. The macro needs to pull the data from specific cells that are the same in each of the 400 workbooks, and paste/transpose them into separate rows on the master spreadsheet.
2. Every sheet is contained within subfolders in the folder "C:\Users\ta3991\Documents\Patch-clamp" on my PC.
3. Every excel sheet that I want to pull data from contains a sheet entitled "Summary data (all cells)".
4. The cells that need to be copied from each of the 400 workbooks are A1, B2:D2, B3:D3, B6:D6
5. The cells should be pasted so all the information from one column in one workbook is a row on the master sheet. I.e.
Workbook 1 cell A1 into P1, B2:D2 into Q2:Q4, B3:D3 into R2:R4, B6:D6 into S2:S4. Workbook 2 cell A1 into P1, B2:D2 into Q5:Q7, B3:D3 into R5:R7, B6:D6 into S5:S7
Etc. (so that cells from columns B-D from the sheet "Summary data (all cells)" from every workbook within "C:\Users\ta3991\Documents\Patch-clamp" is shown paste-transposed as its own row in the master workbook).
6. If its possible, I would also like the name of each of the 400 files to appear in column O on the master workbook alongside the data that has come from that file. I have no idea how to do this bit so I have not included it in my attempt so far.
Any help at all would be greatly appreciated!
Macro attempt so far:
Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim fileName As String
Dim ws As Worksheet
Dim counter As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Set summarysheet to activeworkbook/activesheet where the macro runs
Set SummarySheet = ActiveWorkbook.ActiveSheet
'Select Folder
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = "C:\Users\Natasha\Documents\PhD\Patch-clamp"
If .Show = -1 Then
FolderPath = .SelectedItems(1) & ""
Else
Exit Sub 'User Canceled
End If
End With
' Call Dir the first time, pointing it to all Excel files in the folder path.
fileName = Dir(FolderPath & "*.xlsx*")
Application.ScreenUpdating = False
' Loop until Dir returns an empty string.
Do While fileName <> ""
' Open a workbook in the folder
With Workbooks.Open(FolderPath & fileName)
' Set the source worksheet
Set ws = Nothing
On Error Resume Next
Set ws = .Sheets("Summary data (all cells)")
On Error GoTo 0
If Not ws Is Nothing Then
NextRow = SummarySheet.Range("A" & Rows.Count).End(xlUp).Row + 1
' Copy over the values from the source to the destination next row.
ws.Range("A1").Copy
SummarySheet.Range("P1").PasteSpecial Paste:=xlPasteValues, Transpose:=False
ws.Range("B2:D2").Copy
SummarySheet.Range("Q2" & NextRow).PasteSpecial Paste:=xlPasteValues, Transpose:=True
ws.Range("B3:D3").Copy
SummarySheet.Range("R2" & NextRow).PasteSpecial Paste:=xlPasteValues, Transpose:=True
ws.Range("B6:D6").Copy
SummarySheet.Range("S2" & NextRow).PasteSpecial Paste:=xlPasteValues, Transpose:=True
counter = counter + 1
End If
' Close the source workbook without saving changes.
.Close SaveChanges:=False
End With
' Use Dir to get the next file name.
fileName = Dir()
Loop
Application.ScreenUpdating = True
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
MsgBox counter & " workbooks consolidated. ", , "Consolidation Complete"
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub