I am trying to copy and merge cells in the same excel workbook from one sheet to another. The requirement is as below:
Sheet1 has the following columns=A,B,C,D,E,F,G,H,I,J,K,L
Copy Columns F,G,D,E,K,B from Sheet1 to Sheet 2 in columns A,B,C,D,E,F
Then merge the cells A,B,C,D,E,F in Sheet 2 to column G in sheet2
Since I have to do this same on many sheets, I am looking to do it through powershell.
I have the below code so far, however it is only copying the columns in the same order as Sheet1:
Param(
$path = “C:\Users\Book2.xlsx”,
$worksheet1 = “Sheet1”,
$range1 = “B1:A1”,
$worksheet2 = 2,
$range2 = “A1”
) #end param
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$Workbook = $excel.Workbooks.open($path)
$Worksheet = $Workbook.WorkSheets.item($worksheet1)
$worksheet.activate()
$range = $WorkSheet.Range($range1).EntireColumn
$range.Copy() | out-null
$Worksheet = $Workbook.Worksheets.item($worksheet2)
$Range = $Worksheet.Range($range2)
$Worksheet.Paste($range)
$workbook.Save()
$Excel.Quit()