excel - PowerShell - 找到起始行后从 Excel 中删除一系列行

标签 excel powershell

我有一个 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/

相关文章:

mysql - 如何使用PowerShell脚本将.sql文件导入MySQL?

azure - 如何将 azure VM 镜像移动到其他位置

excel - SSAS 2005、Excel 2007、参差不齐的层次结构

在 PS v4 上运行时,Powershell v2.0 Foreach 循环失败

powershell - 是否可以在 powershell 3.0 的 Invoke-WebRequest 中使用 document.querySelector?

php - exec ('script.ps1' ) 返回错误,cmd> script.ps1 运行成功

arrays - 使用数组比较两个工作表

C# Interop 定义工作表数组

excel - VBA Excel : Is it possible to count horizontal page breaks in a named range?

vba - 应用程序定义或对象定义的错误 Ubound