Combining Multiple Books and removing some rows

bearstronaut

New Member
Joined
Jun 12, 2019
Messages
1
Hi,
I've been looking for a solution to my problem for quite some time and am nearly there. The script I have compiles all workbook sheets in a directory into one workbook. Unfortunately the data I have (over 200 workbooks) has a few extra rows at the start of each sheet I need to get rid of for my other scripts to work. Any suggestions on how to bulk remove the first 2 rows of each copied sheet would be much appreciated.

I have:

Sub mergeFiles()
'Merges all files in a folder to a main file.
'use consolidation function to combine sheets.


'Define variables:
Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As fileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet

Set mainWorkbook = Application.ActiveWorkbook
Set tempFileDialog = Application.fileDialog(msoFileDialogFilePicker)

'Allow the user to select multiple workbooks
tempFileDialog.AllowMultiSelect = True

numberOfFilesChosen = tempFileDialog.Show

'Loop through all selected workbooks
For i = 1 To tempFileDialog.SelectedItems.Count

'Open each workbook
Workbooks.Open tempFileDialog.SelectedItems(i)

Set sourceWorkbook = ActiveWorkbook

'Copy each worksheet to the end of the main workbook
For Each tempWorkSheet In sourceWorkbook.Worksheets
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
Next tempWorkSheet

'Close the source workbook
sourceWorkbook.Close
Next i

End Sub


Thanks in advance
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
Try this

Code:
Sub mergeFiles()
    'Merges all files in a folder to a main file.
    'use consolidation function to combine sheets.
    'Define variables:
    Dim numberOfFilesChosen, i As Integer
    Dim tempFileDialog As FileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook
    Dim tempWorkSheet As Worksheet
    
    Set mainWorkbook = Application.ActiveWorkbook
    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True
    numberOfFilesChosen = tempFileDialog.Show
    
    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.Count
        'Open each workbook
        Workbooks.Open tempFileDialog.SelectedItems(i)
        Set sourceWorkbook = ActiveWorkbook
        'Copy each worksheet to the end of the main workbook
        For Each tempWorkSheet In sourceWorkbook.Worksheets
[COLOR=#0000ff]            tempWorkSheet.Rows("1:2").Delete[/COLOR]
            tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
        Next tempWorkSheet
        'Close the source workbook
        sourceWorkbook.Close [COLOR=#0000ff]False[/COLOR]
    Next i
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,118,857
Messages
5,574,679
Members
412,612
Latest member
Shotokan
Top