Copy data from multiple excel files into one master workbook. However, the data should be added worksheet wise using powershell script

0

I am trying to copy data using PowerShell script from multiple excel files, each excel file has 10 worksheets and the data must be added to the master file worksheet-wise for each excel file.

My code works for a single file copy and add, but when I try to do it from a folder from multiple files it doesn't work.

All the excel files have similar worksheet names (1, 2A, 2B, 3, 4A, 4B, 5A, 5B, 6, 7, 8)

Code working for a single input file

$file1 = 'D:\excel project\raw\t\abc.xlsx' # source's fullpath
$file2 = 'D:\excel project\raw\t\master.xlsx' # destination's fullpath
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$wb2 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
$wb1 = $xl.workbooks.open($file2) # open target
$sh1_wb1 = $wb1.sheets.item('1') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('1') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('2A') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('2A') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('2B') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('2B') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('3') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('3') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('4A') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('4A') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('4B') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('4B') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('5A') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('5A') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('5B') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('5B') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('6') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('6') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('7') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('7') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('8') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('8') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$wb2.close($false) # close source workbook w/o saving
$wb1.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

When I try to do it for multiple source files my code throws multiple errors "You cannot call a method on a null-valued expression.","The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"

Code for multiple files is below

$Files = Get-ChildItem 'D:\excel project\raw\t' | ?{$_.Extension -Match "xlsx?"} | Select -ExpandProperty FullName # source's fullpath
$file2 = 'D:\excel project\raw\t\master.xlsx' # destination's fullpath
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user

ForEach($File in $Files){
$wb2 = $xl.workbooks.open($File, $null, $true) # open source, readonly
foreach($worksheet in $wb2.WorkSheets)
{
$wb1 = $xl.workbooks.open($file2) # open target
$sh1_wb1 = $wb1.sheets.item('1') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('1') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('2A') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('2A') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('2B') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('2B') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('3') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('3') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('4A') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('4A') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('4B') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('4B') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('5A') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('5A') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('5B') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('5B') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('6') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('6') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('7') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('7') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$sh1_wb1 = $wb1.sheets.item('8') # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('8') # source sheet to copy
}
}
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$wb2.close($false) # close source workbook w/o saving
$wb1.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

End result should add the data from multiple excel files, from each worksheet to the master worksheet (eg 2A should be added to master 2A, 2B data should be added to 2B in master, likewise)

will appreciate your guidance and help!!

excel
powershell
asked on Stack Overflow Mar 18, 2021 by Israr

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0