我有一个 PowerShell 脚本,用于在 Excel 文件中搜索特定值(“#N/A”,来自失败的 VLOOKUP),该值需要快速删除所有行,包括该行。
到目前为止我做了什么:
$sheetRange = $sheetData.UsedRange
$colRange = $sheetData.Range("F1")
[void] $sheetRange.Sort($colRange)
$fileWorking.Save()
$strFinder = "#N/A"
$rngSearch = $sheetData.Range("F1").EntireColumn
$txtFound = $rngSearch.Find($strFinder)
$RowFound = ($txtFound.Row)
for (($i = $RowFound); ($i -le $rowsTrans); $i++)
{
$currRow = $sheetData.Cells.Item($i).EntireRow
[void]$currRow.Delete()
}
$fileWorking.Save()
问题是,有超过 200k 行,我需要 删除整个范围而不是一次一行。虽然我对语法有些摸不着头脑,但我不能完全弄清楚实现这一点的正确语法或方法。
显然,
for
循环只需要走,我需要设置 $RowFound
之间的整个范围和 $rowsTrans
......但这是怎么做到的?
最佳答案
我知道这已经很老了,但我找到了一种更好的方法来用单线做到这一点。这篇文章引发了我长达数月的单行之旅……哈哈
Measure-Command { $sheet.UsedRange.SpecialCells([Microsoft.Office.Interop.Excel.XlCellType]::xlCellTypeFormulas, 16).EntireRow.Delete() } #finds any row with an #N/A and blows it away
Referenced from here.注释说明了它的作用,但它会在整个文件中搜索带有#N/A 的任何单元格。我确信有一种方法可以通过缩小到单列来提高效率。希望这可以帮助像我这样无法使用 range 命令的人!
顺便说一句,这将我在约 30,000 行文件上的删除时间从 12-15 小时降低到 30 分钟。
关于excel - PowerShell - 找到起始行后从 Excel 中删除一系列行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486638/