Before you can run the script you need to setup the configuration (MergeExcel.txt) file. In Windows Explorer hold shift and right-click on the file you want to merge, select "Copy as path". Paste the path into MergeExcel.txt file. Each file in the file is the path to the excel file to be merged. The configuration has to reside in the same folder as the VBS script.
c:\folder1\Excel1.xlsx
c:\folder1\Excel2.xlsx
c:\folder3\Excel3.xlsx
Double click to run MergeExcel.vbs. The script will read MergeExcel.txt file located in the same folder and imports all worksheets into one workbook. The script is using VBA to open Excel and import worksheets.
Set fso = CreateObject("Scripting.FileSystemObject")
sFolderPath = GetFolderPath()
sFilePath = sFolderPath & "\MergeExcel.txt"
If fso.FileExists(sFilePath) = False Then
MsgBox "Could not file configuration file: " & sFilePath
WScript.Quit
End If
Dim oExcel: Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oExcel.DisplayAlerts = false
Set oMasterWorkbook = oExcel.Workbooks.Add()
Set oMasterSheet = oMasterWorkbook.Worksheets("Sheet1")
oMasterSheet.Name = "temp_delete"
oMasterWorkbook.Worksheets("Sheet2").Delete
oMasterWorkbook.Worksheets("Sheet3").Delete
Set oFile = fso.OpenTextFile(sFilePath, 1)
Do until oFile.AtEndOfStream
sFilePath = Replace(oFile.ReadLine,"""","")
If fso.FileExists(sFilePath) Then
Set oWorkBook = oExcel.Workbooks.Open(sFilePath)
For Each oSheet in oWorkBook.Worksheets
oSheet.Copy oMasterSheet
'oSht.Move , oSheet
Next
oWorkBook.Close()
End If
Loop
oFile.Close
oMasterSheet.Delete
MsgBox "Done"
Function GetFolderPath()
Dim oFile 'As Scripting.File
Set oFile = fso.GetFile(WScript.ScriptFullName)
GetFolderPath = oFile.ParentFolder
End Function