简体   繁体   中英

Excel 2007 VBA: delete row if cell value is between 600 and 699

I've been struggling with this question for a while. I am trying to write the VBA code to finish this multi-worksheet macro and I'll say thank you in advance for your time & knowledge. I sincerely appreciate it.

I have a sheet of roughly 10 columns, 4,000 rows. The goal is to search column "E" for values between 600 and 699, and delete any row that has column E with a value between 600 and 699.

Right now I'm using this line to delete a row containing "602":

Dim FoundCell As Range
Set FoundCell = Worksheets("StoreEmployeeImport").Range("E:E").Find(what:=602)
Do Until FoundCell Is Nothing
    FoundCell.EntireRow.Delete
    Set FoundCell = Worksheets("StoreEmployeeImport").Range("E:E").FindNext
Loop

Is there a way to do this for all values between 600 and 650? The 'quick and dirty way" is to write this loop for all fifty possible values between 600 and 650 BUT I know there is a better way. Probably much simpler.

THANKS!

This is how I handle it

'Get the last row, 65000th row and column E
finalRow=cells(65000,5).end(xlup).row
'Loop through data backwards
for i = finalRow to 1 step -1
  'Do your check
  if cells(i,5) > 600 and cells(i,5) < 650 then
    'Delete the row if criteria met
    cells(i,1).entirerow.delete
  end if
next i

Try this:

Sub qwerty()
    Dim N As Long, i As Long
    N = Cells(Rows.Count, "E").End(xlUp).Row
    Application.ScreenUpdating = False
    For i = N To 1 Step -1
        v = Cells(i, "E").Value
        If v > 599 And v < 700 Then
            Cells(i, "E").EntireRow.Delete
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM