使用VBA根据使用公式find的值在Excel中删除行

嘿家伙我试图编写一个代码,删除行使用公式find的值。 问题是#VALUE!是一个#VALUE! ,由于报告的设置,我无法更改。 最后我想删除所有具有#VALUE!#VALUE! 以及Column H中小于0.75的任何行。

我试过的代码如下所示:

 Private Sub CommandButton1_Click() Dim rng As Range, cell As Range, del As Range Set rng = Intersect(Range("H1:H2000"), ActiveSheet.UsedRange) For Each cell In rng If (cell.Value) < .75 Then If del Is Nothing Then Set del = cell Else: Set del = Union(del, cell) End If End If Next cell On Error Resume Next del.EntireRow.Delete End Sub 

任何帮助或提示,将不胜感激。

我的代码是其他答案的替代scheme,它的效率更高,执行速度更快,然后分别删除每一行:)给它一个去

 Option Explicit Sub DeleteEmptyRows() Application.ScreenUpdating = False Dim ws As Worksheet Dim i&, lr&, rowsToDelete$, lookFor$, lookFor2$ '*!!!* set the condition for row deletion lookFor = "#VALUE!" lookFor2 = "0.75" Set ws = ThisWorkbook.Sheets("Sheet1") lr = ws.Range("H" & Rows.Count).End(xlUp).Row ReDim arr(0) For i = 1 To lr If StrComp(CStr(ws.Range("H" & i).Text), lookFor, vbTextCompare) = 0 Or _ CDbl(ws.Range("H" & i).Value) < CDbl(lookFor2) Then ReDim Preserve arr(UBound(arr) + 1) arr(UBound(arr) - 1) = i End If Next i If UBound(arr) > 0 Then ReDim Preserve arr(UBound(arr) - 1) For i = LBound(arr) To UBound(arr) rowsToDelete = rowsToDelete & arr(i) & ":" & arr(i) & "," Next i ws.Range(Left(rowsToDelete, Len(rowsToDelete) - 1)).Delete Shift:=xlUp Else Application.ScreenUpdating = True MsgBox "No more rows contain: " & lookFor & "or" & lookFor2 & ", therefore exiting" Exit Sub End If If Not Application.ScreenUpdating Then Application.ScreenUpdating = True Set ws = Nothing End Sub 

我build议在行后退,这样当一行被删除时,你不会失去你的位置。

假设你想查看H列中的单元格,你可以这样做:

 Sub Example() Const H As Integer = 8 Dim row As Long For row = ActiveSheet.UsedRange.Rows.Count To 1 Step -1 On Error Resume Next If Cells(row, H).Value < 0.75 Then Rows(row).Delete End If On Error GoTo 0 Next End Sub 

尝试:

 Private Sub CommandButton1_Click() Dim rng As Range, cell As Range, del As Range, v As Variant Set rng = Intersect(Range("H1:H2000"), ActiveSheet.UsedRange) For Each cell In rng v = cell.Text If v < 0.75 Or v = "#VALUE!" Then If del Is Nothing Then Set del = cell Else: Set del = Union(del, cell) End If End If Next cell On Error Resume Next del.EntireRow.Delete End Sub