Delete a range of Rows based on a cell value

niha

New Member
Joined
May 6, 2021
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hi Guys!
When I get "Completed" in column K, it becomes the active cell. After this, I want to delete the rows from the completed cell row till the row where I find the next non-empty cell in column D. In this case I want to delete rows from 8 to 12. This number would always vary.
 

Attachments

  • Capture.JPG
    Capture.JPG
    68.3 KB · Views: 13

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Based on your sample you posted, can you post what the desired output should look like?
 
Upvote 0
Based on your sample you posted, can you post what the desired output should look like?
These rows have been deleted and rows below it have shifted up. Something like this.
 

Attachments

  • DE.JPG
    DE.JPG
    60.6 KB · Views: 9
Upvote 0
OK, you did not give us a whole lot of data, so I made a few assumptions:
- Every instance where "Completed" is in column K has an entry in column D of that line.
- There is at least one blank row in column D between every entry.

Based on that, I think this code will do what you want:
VBA Code:
Sub MyDeleteRows()

    Dim lr As Long
    Dim r As Long
    Dim nr As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column G with data
    lr = Cells(Rows.Count, "G").End(xlUp).Row
    
'   Loop through rows backwards from bottom to top
    For r = lr To 8 Step -1
'       Check to see if column K has "Completed" in it
        If Cells(r, "K") = "Completed" Then
'           Find bottom row to deletem using column D
            nr = Cells(r, "D").End(xlDown).Row
'           Make sure it doesn't go past last row
            If nr > lr Then nr = lr
'           Delete rows
            Rows(r & ":" & nr - 1).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,014
Messages
6,122,697
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top