Delete row if it contains certain text with VBA

Dokat

Active Member
Joined
Jan 19, 2015
Messages
304
Office Version
  1. 365
Hi

I have a worksheet ("Region") with over 300K rows of data . I'd like to delete entire rows where intersecting column F value is "Total USA" with VBA.

For Ex: Delete row 4 if F4 is "Total USA".

I have below code however its giving me Runtime error 424. Appreciate any help modifying code. Thanks


1646100466389.png


VBA Code:
Sub RowTotalUSA()
  Dim i As Long
 
  Application.ScreenUpdating = False
  With Sheets("Region")
    For i = .UsedRange.Row.Count To 1 Step -1
      If .Cells(Column.Count, i).End(xlUp).Column = "Total USA" Then .Row(i).Delete
    Next i
  End With
  Application.ScreenUpdating = True
End Sub
 
This will loop through column F, then collect row index where "Total USD" is found, then delete entire row at one:
VBA Code:
Option Explicit
Sub test()
Application.ScreenUpdating = False
Dim Lr&, i&, st As String
With Sheets("Region")
    Lr = .Cells(Rows.Count, "F").End(xlUp).Row
    For i = 1 To Lr
        If Cells(i, "F").Value Like "Total USA" Then
            st = st & "," & i & ":" & i ' collect rows index
        End If
    Next
    st = Mid(st, 2, 255)
.Range(st).Delete
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Thank you for your reply.
This will loop through column F, then collect row index where "Total USD" is found, then delete entire row at one:
VBA Code:
Option Explicit
Sub test()
Application.ScreenUpdating = False
Dim Lr&, i&, st As String
With Sheets("Region")
    Lr = .Cells(Rows.Count, "F").End(xlUp).Row
    For i = 1 To Lr
        If Cells(i, "F").Value Like "Total USA" Then
            st = st & "," & i & ":" & i ' collect rows index
        End If
    Next
    st = Mid(st, 2, 255)
.Range(st).Delete
End With
Application.ScreenUpdating = True
End Sub
When i tried thecode i received below error message. JoeMOe's code worked earlier i am all set. Thank you

1646103800678.png
 
Upvote 0
VBA Code:
lrow: Worksheets("Region").Range("F" & Rows.Count).End(xlUp).Row

equates to:
VBA Code:
lrow
Worksheets("Region").Range("F" & Rows.Count).End(xlUp).Row

Also:
VBA Code:
If Cells(iCntr, 1) = "Total USA" Then

The 1 in that line refers to column 1 which is the A column. Remember I said cells uses a Row,Column format. :)
Then should i replace cells with row?
 
Upvote 0
You would replace the '1' with '6', signifying the the 6th column which = F.
 
Upvote 0
You would replace the '1' with '6', signifying the the 6th column which = F.
I tried that too however received error message. JoeMoe's code worked earlier. I am all set. Thanks for your help.

1646104554639.png
 
Upvote 0
I don't see how you can get that error if you have a sheet named Region in the same workbook.

VBA Code:
Sub DeleteUSA()

Dim lrow As Long
Dim icntr As Long

lrow = Worksheets("Region").Range("F" & Rows.Count).End(xlUp).Row


For icntr = lrow To 1 Step -1

If Cells(icntr, 6) = "Total USA" Then
         
        Rows(icntr).Delete
   
    End If

Next

End Sub

Maybe you want to use 'ActiveSheet' ?
 
Upvote 0
I don't see how you can get that error if you have a sheet named Region in the same workbook.

VBA Code:
Sub DeleteUSA()

Dim lrow As Long
Dim icntr As Long

lrow = Worksheets("Region").Range("F" & Rows.Count).End(xlUp).Row


For icntr = lrow To 1 Step -1

If Cells(icntr, 6) = "Total USA" Then
        
        Rows(icntr).Delete
  
    End If

Next

End Sub

Maybe you want to use 'ActiveSheet' ?
I am nit sure why. Please see below screenshot.

1646105765454.png
 
Upvote 0
Exactly! You don't have a sheet named Region.

Change the Sheet1 name to Region ... Or change the Region in the codes to Sheet1.
 
Upvote 0
Try the following:

VBA Code:
Sub DeleteUSA()

Dim lrow As Long
Dim icntr As Long

lrow = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row


For icntr = lrow To 1 Step -1

If Cells(icntr, 6) = "Total USA" Then
          
        Rows(icntr).Delete
    
    End If

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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