用较小的数字值删除行

对下面的macros有点麻烦。 这个想法是,它将查看“粘贴”工作表上的logging,并与“LastRun”表中“B1”中保存的数字进行比较,如果数字较小,则删除该行。 在运行macros时,它被卡在'结束如果'部分。

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 

任何帮助表示赞赏。

请试试这个:

 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 

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

 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