gpt4 book ai didi

vba - 删除列中所有有错误的行

转载 作者:行者123 更新时间:2023-12-03 01:57:53 24 4
gpt4 key购买 nike

我正在编写 VBA 代码,需要部分代码来删除工作表中“J”列中的文本为“#N/A”的所有行。我已经编写了代码,但在调试时出现类型不匹配错误。

这是代码

Dim i As Long
For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1
If Cells(i, 10) = "#N/A" Then Cells(i, 1).EntireRow.Delete
Next i

最佳答案

使用Range.SpecialCells methodxlSpecialCellsValue 常量设置为 xlErrors,以快速识别 J 列中存在错误的所有单元格。没有透露这些单元格是公式还是键入的常量,因此我添加了代码来通过xlCellType Enumeration检查两者。类型。

Sub del_error_rows()
With Worksheets("Sheet3")
On Error Resume Next
With .Columns(10).SpecialCells(xlCellTypeFormulas, xlErrors)
.EntireRow.Delete
End With
With .Columns(10).SpecialCells(xlCellTypeConstants, xlErrors)
.EntireRow.Delete
End With
On Error GoTo 0
End With
End Sub

如果不存在具有该特定错误配置的单元,则需要On Error Resume Next。在这种情况下,SpecialCell 将为 Nothing,并且您必须绕过因尝试不处理任何内容而引发的任何错误。

关于vba - 删除列中所有有错误的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34638011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com