I am basically trying to compare a cell within Excel against another cell within another worksheet using PowerShell. This is the code I am using:
# Define location
$crs = "C:\temp\CRSENGCY_PS.xlsx"
$english = "English"
$welsh = "Welsh"
$SubSection = "SubSection"
# Create instance
$objExcel = New-Object -ComObject Excel.Application
$workBook = $objExcel.Workbooks.Open($crs)
$englishSheet = $workBook.Worksheets.Item($english)
$subSectionSheet = $workBook.Worksheets.Item($SubSection)
$objExcel.Visible = $false
# Num of rows
$engRowMax = 1812
$subRowMax = 677
# Define columns
$rowName, $colName = 1, 1
for ($i=1; $i -le $subRowMax; $i++) {
$SubSectionName = $subSectionSheet.Cells.Item($rowName+$i,2).Text
$3SubSections = $SubSectionName.Substring(0, 3)
for ($i=1; $i -le $engRowMax; $i++) {
$englishName = $englishSheet.Cells.Item($rowName+$i, $colName).Text
$3englishName = $englishName.Substring(0, 3)
if ($3englishName -eq $3SubSections) {
Write-Host("Success")
} else {
Write-Host("Failed" + $3SubSections + " " + $3englishName)
}
}
}
$objExcel.Quit()
The issue I have is that the for loop at the bottom only runs once. The for loop inside runs the correct number of times. If I remove the nested for loop it works fine.
$i) in nested loops. Use a different variable for each loop.