I tried to make a script to copy from one workbook sheet to another and paste in the exact same range. I tried five different solutions but failed yet.
$excel=new-object -comobject excel.application;
$excel.visible=$true;
$SourceWorkBook=$Excel.Workbooks.open("C:\Oldsheet.xlsx");
$TargetWorkBook=$excel.workBooks.open("C:\Newsheet.xlsx");
$SourceWorkBook.WorkSheets.item("Numbers").activate();
$SourceRange=$SourceWorkBook.WorkSheets.item("Numbers").range("B2:G400");
$SourceRange.copy() | out-null;
$TargetWorkBook.worksheets.item("Numbers").paste("B2:G400");
My code above opens the workbooks and selects the range for copy but doesn't paste it itself. In addition I would like to suppress error messages ($Excel.DisplayAlerts = $False didn't work) and save the file after pasting.