Good evening,
I have the following VbScript code that reads multiple excel files and copies all the data and paste it to another workbook. I am getting an unkown runtime error on the .PasteSpecial please help. Error line highlighted in blue.
Option Explicit
Dim Excel, fso, strPathSrc, file, folder, Workbook, NRow, FileName, LastRow, Sheet, DestRange, SourceRange, WshShell, strDesktop, listfile, x, Workbook2, Sheet2, a, LastCol
Dim ArrayList, i
Const xlUp = -4162
Const xlDown = -4121
Const xlToRight = -4161
Const xlPasteValues = -4163
Set fso = CreateObject("Scripting.FileSystemObject")
Set Excel = CreateObject("Excel.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
strPathSrc = "Z:\"
Set Workbook2 = Excel.Workbooks.Add()
Set Sheet2 = Workbook2.WorkSheets(1)
Set Folder = fso.GetFolder(strPathSrc)
For each file in Folder.SubFolders
For each x in file.Files
If NRow = 0 Then
NRow = 4
End if
FileName = x.path
Set Workbook = Excel.Workbooks.Open(FileName)
Set Sheet = Workbook.WorkSheets(1)
'LastCol = Sheet.Range("A4").End(xlToRight).Column
LastRow = Sheet.Range("A4:H" & Sheet.Rows.Count).End(xlDown).Row
SourceRange = Sheet.Range("A4:H" & LastRow).copy
'Set SourceRange = Sheet.Range("A4", Sheet.Cells(LastRow, LastCol)).copy
Sheet2.Range("A4", SourceRange).PasteSpecial -4163, -4142, True, False
Workbook.Close
Next
Next
Excel.DisplayAlerts = False
Workbook2.SaveAs(strDesktop & "\Effeciency_Merge.xlsx")
MsgBox "Copy Complete"
I have the following VbScript code that reads multiple excel files and copies all the data and paste it to another workbook. I am getting an unkown runtime error on the .PasteSpecial please help. Error line highlighted in blue.
Option Explicit
Dim Excel, fso, strPathSrc, file, folder, Workbook, NRow, FileName, LastRow, Sheet, DestRange, SourceRange, WshShell, strDesktop, listfile, x, Workbook2, Sheet2, a, LastCol
Dim ArrayList, i
Const xlUp = -4162
Const xlDown = -4121
Const xlToRight = -4161
Const xlPasteValues = -4163
Set fso = CreateObject("Scripting.FileSystemObject")
Set Excel = CreateObject("Excel.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
strPathSrc = "Z:\"
Set Workbook2 = Excel.Workbooks.Add()
Set Sheet2 = Workbook2.WorkSheets(1)
Set Folder = fso.GetFolder(strPathSrc)
For each file in Folder.SubFolders
For each x in file.Files
If NRow = 0 Then
NRow = 4
End if
FileName = x.path
Set Workbook = Excel.Workbooks.Open(FileName)
Set Sheet = Workbook.WorkSheets(1)
'LastCol = Sheet.Range("A4").End(xlToRight).Column
LastRow = Sheet.Range("A4:H" & Sheet.Rows.Count).End(xlDown).Row
SourceRange = Sheet.Range("A4:H" & LastRow).copy
'Set SourceRange = Sheet.Range("A4", Sheet.Cells(LastRow, LastCol)).copy
Sheet2.Range("A4", SourceRange).PasteSpecial -4163, -4142, True, False
Workbook.Close
Next
Next
Excel.DisplayAlerts = False
Workbook2.SaveAs(strDesktop & "\Effeciency_Merge.xlsx")
MsgBox "Copy Complete"