How do I compare cells in excel using powershell

I would like to compare the cells in one excel spread sheet to match the second spread sheet.

If ID in new xlsx = ID in the old xlsx

Thanks

July 1st, 2013 4:07pm

How do I copy cells that are in one xlsx file to another one that already exists. Lets say test1.xlsx has a name colunm and I want to copy that name in to another file test2.xlsx,  

Thanks

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 4:59pm

Hi,

Please read the following:

Bill

July 1st, 2013 5:29pm

Cells, Columns and rows all have a 'Copy' method.  Post in the Excel VBA forum to learn how to use Excel object model.  Once you learn how to do it in VBA you can convert what you have learned to PowerShell.

$sheet2.Range(<source range>).Copy(<targetrange>)

You can also look in the Repository (link above) for examples of how to use Excel with PowerShell.

The simplest single cell copy is:

$sheet2.Cells.Item(1,1)=$sheet1.Cells.Item(1,1)

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 8:00pm

This thread shows up with two titles.  One is marked Copy and the other compare.

If the real question is "compare" then the example is correct but need to compare instead of assign.  The cell references for copy are identical to compare.

Start by learning VBA then learn PowerShell then convert the VBA examples to PowerShell. Simple.

July 1st, 2013 8:04pm

you can use this as example... hope this is userful...

$newxlsxfile = "test1.xlsx"
$oldxlsxfile = "test2.xlsx"

$objExcel = new-object -comobject excel.application 
$objExcel.Visible = $True
$objExcel.DisplayAlerts = $False 
$objNewWorkbook = $objExcel.Workbooks.Open($newxlsxfile) 
$objNewWorksheet = $objNewWorkbook.Worksheets.Item(1) 

$objoldWorkbook = $objExcel.Workbooks.Open($oldxlsxfile) 
$objoldWorksheet = $objoldWorkbook.Worksheets.Item(1) 
 
$intRow = 1 
$intCol = 1 
Do{ 
if($objNewWorksheet.Cells.Item($intRow,$intCol).Value().ToLower() -eq $objoldWorksheet.Cells.Item(1,$intCol).Value().trim().ToLower()){
  $objNewWorksheet.Cells.Item($intRow,2).Value() = "Matched"
  }
  else{
  $objNewWorksheet.Cells.Item($intRow,2).Value() = "Not Matched"
  }
$intRow++ 
}While ($objNewWorksheet.Cells.Item($intRow,$intCol).Value() -ne $null) 

$objNewWorkbook.Save
$objNewWorkbook.Close() 
$objoldWorkbook.Close() 
$objExcel.Quit() 

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2013 8:48pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics