Pasting to Excel using Powershell

ExcelOnFire

New Member
Joined
Nov 29, 2016
Messages
1
I am using Powershell to take information from one report and adding it to another. I have been able to achieve this goal with one small hiccup that I cannot figure out. The script will open both workbooks and copy the selected cells. The problem that I am stuck on is how to paste using the destination formatting. This below script will paste but using the source format.

Any help would be greatly appreciated.

Can you take a look at this code, I am having a similar problem with pasting. The code will perform as written but it is keeping the source format and I want the integrity of the destination formatting to be maintained. Any help will be greatly appreciated!

#This opens a new Excel object and makes it visible

$excel
=new-object-comobjectexcel.application;

$excel
.visible=$true;


#This identifies the source workbook (the one from which the data will be copied).

$SourceWorkBook
=$Excel.Workbooks.open("C:\Users\mdelaney\Downloads\CCOPCL b0144535.xlsx");


#This activates the source workbook

$SourceWorkBook
.WorkSheets.item("Page2_2").activate();


#This is a 5 second pause to allow the workbook to open

Start-Sleep
-s5


#This specifies the range that we want to copy

$SourceRange
=$SourceWorkBook.WorkSheets.item("Page2_2").range("R5");


#This copies that range. It will say "true" if the copy is successful, but we're not interested in seeing that.

#Because of this, we will use the out-null cmdlet, which discards any info that is piped to it.

$SourceRange
.copy() |out-null;


#This identifies the target workbook (the one to which the data will be pasted)

$TargetWorkBook
=$excel.workBooks.Open("C:\Users\mdelaney\Desktop\November DC-MM SLA Report.xlsx");


#This is a 5 second pause to allow the workbook to open

Start-Sleep
-s5


#This identifies the destination where we want the information copied.

#Here, it pastes the information in the tab named "SLA Report" of the target workbook

#First, identify where you want it pasted

$TargetRange
=$TargetWorkbook.Worksheets.item("SLA Report").range("B11");


#Then, paste that into the target workbook

$TargetRange
.Activate()|Out-Null; $Excel.ActiveSheet.Paste()


 

 

 

 

#This specifies the range that we want to copy

$SourceRange
=$SourceWorkBook.WorkSheets.item("Page2_2").range("G5");


#This copies that range. It will say "true" if the copy is successful, but we're not interested in seeing that.

#Because of this, we will use the out-null cmdlet, which discards any info that is piped to it.

$SourceRange
.copy() |out-null;


#This identifies the destination where we want the information copied.

#Here, it pastes the information in the tab named "SLA Report" of the target workbook

#First, identify where you want it pasted

$TargetRange
=$TargetWorkbook.Worksheets.item("SLA Report").range("C11");


#Then, paste that into the target workbook

$TargetRange
.Activate()|Out-Null; $Excel.ActiveSheet.Paste()


 

 

 

#This specifies the range that we want to copy

$SourceRange
=$SourceWorkBook.WorkSheets.item("Page2_2").range("N5");


#This copies that range. It will say "true" if the copy is successful, but we're not interested in seeing that.

#Because of this, we will use the out-null cmdlet, which discards any info that is piped to it.

$SourceRange
.copy() |out-null;


#This identifies the destination where we want the information copied.

#Here, it pastes the information in the tab named "SLA Report" of the target workbook

#First, identify where you want it pasted

$TargetRange


=$TargetWorkbook.Worksheets.item("SLA Report").range("D11");


#Then, paste that into the target workbook

$TargetRange


.Activate()|Out-Null; $Excel.ActiveSheet.Paste()
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,214,817
Messages
6,121,720
Members
449,050
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top