Hello all! I am working with the below code to attempt consolidating all of the Excel files within a folder (File1, File2, etc.). I have created and renamed sheets in the blank workbook (named "cExcelFiles") to match the sheets in the files I will be copying data from. The sheets all share the same first column so that has been copied and pasted manually into "cExcelFiles". The code breaks at Range.Copy Destination.
Ideally, this macro would cycle through each Excel file in the folder and copy columns B & C (maybe there's also a way to only copy down to the last row used in the columns) from each sheet and paste this over in the sheet of the same name in "cExcelFiles" and I want to know how I can get it so that as it loops through the Excel files, it will go into the next empty column to paste the next file's columns, if that makes sense. So File1 'SheetA' columns B&C would go into "cExcelFiles" 'SheetA' columns B&C, File2 'SheetA' columns B&C would go into "cExcelFiles" 'SheetA' columns D&E, File3 'Sheet A' columns B&C would go into "cExcelFiles" 'Sheet A' columns F&G, etc.
I am not an expert in VBA by any means but have had some experience working with it and would appreciate any help at all in how to go about this. Thanks so much!
Ideally, this macro would cycle through each Excel file in the folder and copy columns B & C (maybe there's also a way to only copy down to the last row used in the columns) from each sheet and paste this over in the sheet of the same name in "cExcelFiles" and I want to know how I can get it so that as it loops through the Excel files, it will go into the next empty column to paste the next file's columns, if that makes sense. So File1 'SheetA' columns B&C would go into "cExcelFiles" 'SheetA' columns B&C, File2 'SheetA' columns B&C would go into "cExcelFiles" 'SheetA' columns D&E, File3 'Sheet A' columns B&C would go into "cExcelFiles" 'Sheet A' columns F&G, etc.
I am not an expert in VBA by any means but have had some experience working with it and would appreciate any help at all in how to go about this. Thanks so much!
Code:
Option Explicit
Sub CombineFiles()
Dim FolderPath As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Dim FolderPicker As Object
Dim FilesInPath As String
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim intChoice As Integer
Set FolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
FolderPicker.AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = FolderPicker.Show
'determine what choice the user made
If intChoice <> 0 Then
'get the folder path selected by the user
FolderPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
Else: End
End If
' Add a slash at the end of the path if needed.
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(FolderPath & "*.xls*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
Do While FilesInPath <> ""
Set Wkb = Workbooks.Open(FolderPath & FilesInPath)
For Each WS In Wkb.Worksheets
Range("B:C").Copy Destination:=Worksheets("Sheet1").Range("B1")
Next WS
Wkb.Close False
FilesInPath = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub