Forum Discussion
B Picazo
Nov 12, 2017Copper Contributor
Consolidating all worksheets (different workbook) into one workbook
Hi,
I've quite new in using macro and have only learned via blogs/youtube.
I've been using this macro to consolidate my data from different workbooks into one. However, if a cell is hidden, the hidden cells are not considered in the count of cells to be copied (ex. copy A1 to D1, but C1 is hidden, it will copy A1,B1,D1 & E1 instead).
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, filepath As String, filename As String
FolderPath = "C:\Users\bbp\Desktop\Excel Conso\"
filepath = FolderPath & "*.xlsx"
filename = Dir(filepath)
Dim lastrow As Long, lastcolumn As Long
Do While filename <> ""
Workbooks.Open (FolderPath & filename)
Range("A4:AL4").Copy
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(4, 1), Cells(lastrow, 38)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets("Sheet 1").Range(Cells(erow, 1), Cells(erow, 38))
filename = Dir
Loop
End Sub
Hope you could help me.
Thank you.
Hi
I'd recommend you take a look at Power Query for consolidating data from files
(assuming you have Excel 2010 or later)
I used to use VBA to do what you are doing but now Power Query is much simpler
Here's a slightly out of date video of how it works, these days Power Query makes it even easier ot combine files with the Combine + Edit option
https://accessanalytic.com.au/power-query-get-transform-introduction-part-1/