Using VBA to remove a row which contains a keyword

dleach19

New Member
Joined
Jun 30, 2014
Messages
7
All,

I have a workbook which I am using to track a large number of deals. Column G in the Tracking Report provides a "Deal Status". I was hoping someone could help me with the VBA code to do the following:

1) go to the last active row in Column G on the Tracking Report worksheet

2) Begin Looping through the rows and identifying if Column G has the Deal Status as "Dead"

3) If the Deal Status is "Dead" Cut the entire row and move it to the "Dead Deals" sheet

4) If the status is anything but "Dead" leave it alone and move on to the next one

Any help would be greatly appreciated!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to the board.

If there are no formulas in column G try this, there's no need to loop.
Code:
Sub RemoveDeadDeals()
Dim lR As Long
lR = Range("G" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("G1:G" & lR).Replace what:="Dead", replacement:="#N/A", MatchCase:=False
On Error Resume Next
Range("G1:G" & lR).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Welcome to the board.

If there are no formulas in column G try this, there's no need to loop.
Rich (BB code):
Sub RemoveDeadDeals()
Dim lR As Long
lR = Range("G" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("G1:G" & lR).Replace what:="Dead", replacement:="#N/A", MatchCase:=False
On Error Resume Next
Range("G1:G" & lR).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
I do not think that does what the OP asked for... see item #3 in his list.
 
Upvote 0
Hi,

If we assume that our Active sheet where form we look for the Dead deals is called "All Deals"

I would recommend to filter the raw data in column G, copy the rows to the "Dead Deals" sheet and then clear up the "All Deals" sheet (delete the rows).
Code:

Sub FilterDeadDeals()


Dim lastrow As Long
Dim lastrow2 As Long


'Filter the column "G" (number 7) with our criteria which is "Dead"
lastrow = ActiveSheet.Range("G" & Rows.Count).End(xlUp).Row
Sheets("All Deals").Range("A1").AutoFilter field:=7, Criteria1:="Dead"

'Copy the filtered rows
Sheets("All Deals").Rows("2:" & lastrow).Copy


'Activate the new sheet
Sheets("Dead Deals").Activate

'Find the last used row in the new spreadsheet
lastrow2 = ActiveSheet.Range("G" & Rows.Count).End(xlUp).Row + 1

'Paste on the first free cell
ActiveSheet.Range("A" & lastrow2).PasteSpecial xlPasteAll

'Activate the "All Deals" sheet and delete the "Dead" deals rows
Sheets("All Deals").Activate
Sheets("All Deals").Rows("2:" & lastrow).EntireRow.Delete

'Display all data
Sheets("All Deals").ShowAllData


End Sub



Feel Free to change the "All Deals" Sheet naming to whatever suits your needs (or anything else).

Cheers.
 
Upvote 0
Wallabiee,

Thank you for the detailed response. When I run your code I get an error (Subscript out of range) on this line item:

'Filter the column "G" (number 7) with our criteria which is "Dead"
lastrow = ActiveSheet.Range("G" & Rows.Count).End(xlUp).Row
Sheets("All Deals").Range("A1").AutoFilter field:=7, Criteria1:="Dead"

Any thoughts?
 
Upvote 0
Try running this with the sheet containing your data to be cut active:
Code:
Sub CutDeadDeals()
'Assumes headers in row 1
Dim lR As Long, R As Range, n As Long
lR = Range("G" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Set R = Range("G1:G" & lR).CurrentRegion
R.AutoFilter field:=7, Criteria1:="Dead"
Set R = R.Rows("2:" & lR)
R.Copy
With Sheets("Dead Deals")
    lR = .Range("A" & Rows.Count).End(xlUp).Row + 1
    .Range("A" & lR).PasteSpecial Paste:=xlPasteValues
End With
On Error Resume Next
R.Delete shift:=xlUp
Range("G1").AutoFilter
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Wallabiee,

Thank you for the detailed response. When I run your code I get an error (Subscript out of range) on this line item:

'Filter the column "G" (number 7) with our criteria which is "Dead"
lastrow = ActiveSheet.Range("G" & Rows.Count).End(xlUp).Row
Sheets("All Deals").Range("A1").AutoFilter field:=7, Criteria1:="Dead"

Any thoughts?


Hi,

Is your sheet with the same name like "All Deals"?

Cheers.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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