删除数值较小的行

时间:2017-09-29 10:04:40

标签: excel vba

使用下面的宏有点麻烦。我们的想法是,它会查看“粘贴”中的记录。表格并与' B1'中保存的数字进行比较关于' LastRun'如果数字较低,请单击并删除该行。在运行宏时,它会卡在“结束时”上。部分。

Sub DeleteOld()
    Worksheets("Paste").Activate

        endrow = Sheets("Paste").Range("X2000").End(xlUp).Row
        OldData = Sheets("LastRun").Range("B1").Value
        For i = endrow To 2 Step -1
         NewData = Cells(i, 24).Value
             If NewData < OldData Then
             Cells(i, 24).EntireRow.Delete
        End If
    Next i

End Sub

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

请试试这个:

Option Explicit

Sub DeleteOld()

Dim endrow As Long, i As Long
Dim newdata As Double, olddata As Double


olddata = Sheets("LastRun").Range("B1").Value

    With Worksheets("Paste")

        endrow = .Range("X2000").End(xlUp).Row

        For i = endrow To 1 Step -1

         newdata = .Cells(i, 24).Value

             If newdata < olddata Then

             .Cells(i, 24).EntireRow.Delete

             End If

          Next i

   End With

End Sub

答案 1 :(得分:0)

也许这会有所帮助。但是,为了简化起见,它会检查工作表中删除行的第一列中的值。

Public Sub test()
Dim i As Long
Dim lastRow As Long

 Dim rngB2 As Range
 Set rngB2 = Sheets("lastRun").Cells(1, 2)

  With Sheets("paste")
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

  For i = lastRow To 1 Step -1
    Dim curRng As Range
    Set curRng = .Cells(i, 1)
    If curRng.Value < rngB2.Value Then
    .Rows(curRng.Row).Delete
    'curRng.Interior.Color = rgbRed
    End If
  Next i

  End With

End Sub