CutterSoilMixing
New Member
- Joined
- Jun 8, 2019
- Messages
- 17
- Office Version
- 365
- Platform
- Windows
Hello everyone,
I have a VBA code that merges worksheets (named "Data" in this case) from all excel files in a folder into one workbook and creates a new tab for each. I'm struggling with two issues at the moment and would greatly appreciate any input:
1. currently, I have to manually enter the folder path containing the files I want to merge and the files are always in a different folder. I'd like to set this up so that a window pops up that allows me to select the folder.
2. the code copies the entire worksheet from the source, creates a new worksheet in the master and then pastes the data there. If the master already contains Sheet1 and Sheet2 the code overwrites both sheets with data from the source files. Is there a way to tell the code to start inserting new worksheets after sheet1 and sheet2? I've played around with iCnt in the code but no luck...
Thanks you very much in advance and all the best!
I have a VBA code that merges worksheets (named "Data" in this case) from all excel files in a folder into one workbook and creates a new tab for each. I'm struggling with two issues at the moment and would greatly appreciate any input:
1. currently, I have to manually enter the folder path containing the files I want to merge and the files are always in a different folder. I'd like to set this up so that a window pops up that allows me to select the folder.
2. the code copies the entire worksheet from the source, creates a new worksheet in the master and then pastes the data there. If the master already contains Sheet1 and Sheet2 the code overwrites both sheets with data from the source files. Is there a way to tell the code to start inserting new worksheets after sheet1 and sheet2? I've played around with iCnt in the code but no luck...
Thanks you very much in advance and all the best!
VBA Code:
Option Explicit
Private Sub CommandButton1_Click()
mergeData
End Sub
Sub mergeData()
On Error GoTo ErrHandler
Application.ScreenUpdating = False
' Our FileSystem Objects.
Dim objFs As Object
Dim objFolder As Object
Dim file As Object
Set objFs = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFs.GetFolder("INSERT FOLDER PATH CONTAINING FILES TO MERGE HERE") ' The path of the source files.
Dim iCnt As Integer ' Just a counter.
iCnt = 1
' Loop through all the files in the folder.
For Each file In objFolder.Files
Dim objSrc As Workbook ' The source.
Set objSrc = Workbooks.Open(file.Path, True, True)
Dim iTotalRows As Integer ' The total rows used in the source file.
iTotalRows = objSrc.Worksheets("Data").UsedRange.Rows.Count
Dim iTotalCols As Integer ' Now, get the total columns in the source.
iTotalCols = objSrc.Worksheets("Data").UsedRange.Columns.Count
Dim iRows, iCols As Integer
' Read data from source and copy in the master file.
For iRows = 1 To iTotalRows
For iCols = 1 To iTotalCols
Application.Workbooks(1).ActiveSheet.Cells(iRows, iCols) = _
objSrc.Worksheets("Data").Cells(iRows, iCols)
' Note: It will read data in "Sheet1" of the source file.
Next iCols
Next iRows
iRows = 0
' Get the name of the file (I'll name the active sheet with the filename).
Dim sSheetName As String
sSheetName = Replace(objSrc.Name, ".xlsx", "") ' I am assuming the files are .xlsx files.
' Close the source file (the file from which its copying the data).
objSrc.Close False
Set objSrc = Nothing
With ActiveWorkbook
.ActiveSheet.Name = sSheetName ' Rename the sheet.
iCnt = iCnt + 1
If iCnt > .Worksheets.Count Then
' Create or add a new sheet after the last sheet.
.Sheets.Add After:=.Worksheets(.Worksheets.Count)
End If
.Worksheets(iCnt).Activate ' Go to the next sheet.
End With
Next
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub