HI All,
I am using powershell to manipulate an excel spreadsheet.
I am using a vlookup to get data from another sheet and compare it.
I want to delete all rows where the vlookup comes back with #N/A but I am struggling to get it to work.
I tried using this code that I found on a website
for ($i = 1; $i -le $rows; $i++) {
If ($ws.Item($i,5) = "#N/A") {
$Range = $ws.Cells.Item($i, 1).EntireRow
$Range.Delete()
$i = $i - 1
}
}
But that goes through and deletes every row.
does anyone have any ideas?
My code so far is:-
# Show Excel
$xl.visible = $true
$xl.DisplayAlerts = $False
# Create a workbook
$wb = $xl.Workbooks.open("x:\messaging\pstfilestest.csv")
$wb1 = $xl.Workbooks.open("x:\messaging\Batch14a\BAtch14a.csv")
# Get sheets
$ws = $wb.WorkSheets.item(1)
$ws.activate()
Start-Sleep 1
$ws.cells.item(1,3).value() = "Alias"
$ws.Cells.Item(1,4).value() = "UserPrincipalName"
$ws.Cells.Item(1,5).value() = "DisplayName"
$ws.Cells.Item(1,6).value() = "Batch"
$ws.cells.item(2,3).value() = "=LEFT(RC[-1],FIND(""@"",RC[-1])-1)"
$ws.Cells.Item(2,4).value() = "=LEFT(RC[-2],FIND("".uk"",RC[-2])+2)"
$ws.Cells.Item(2,5).value() = "=VLOOKUP(RC[-1],BAtch14a.csv!R1:R1048576,2,FALSE)"
$ws.Cells.Item(2,6).value() = "Batch14a_001"
$rows = $ws.range("c2").currentregion.rows.count
$ws.range("c2:e$rows").formula = $ws.range("c2:e2").formula
[void]$ws.Cells.Item(1,1).select()
[void] $ws.cells.EntireColumn.Autofit()
$Range = $ws.UsedRange
$R2 = $ws.Range("E1")
[void] $Range.Sort($R2)
$Range = $ws.Range("A1:F1").entirecolumn
$Range.Copy()|Out-Null
$Range = $ws.Range("A1")
$Range.PasteSpecial(-4163)
Bottom 4 lines is to paste the values rather than the formula as I thought this might be the issue so I can remove them if needed
TIA
Andy