Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Use powershell to compare columns in excel

$
0
0

I have an excel workbook with two sheets. I want to compare two columns in different sheets using powershell.

Say column C in Sheet 1 needs to be compared to column D in Sheet2. Column C has say 100 cells and Column D has 120 cells.

I want to compare each cell value in Column C with all cells values in ColumnD and vice versa. So far I have something like this, however it does meet the above requirement:

Param(
   $path = "C:\Users\Documents\Book22.xlsx",
   $worksheet1 = “1”,
   $worksheet2="2"
   )
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$Workbook = $excel.Workbooks.open($path)
$ws1 = $Workbook.WorkSheets.item($worksheet1)
$ws2 = $Workbook.WorkSheets.item($worksheet2)
$ws2.activate() 
$intRow=2
$intCol1=5
$inCol2=6
Do{ 
if($ws2.Cells.Item($intRow,$intCol2).Value() -eq $ws1.Cells.Item($intRow,$intCol1).Value()){
  $ws2.Range("($intRow):$($intRow)").interior.colorindex=3
  }
  else{
  $ws2.Range("($intRow):$($intRow)").interior.colorindex=5
  }
$intRow++ 
}While ($objNewWorksheet.Cells.Item($intRow,$intCol).Value() -ne $null) 
$workbook.save()
$excel.Quit()


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>