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
 
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
Thank you. This one worked. Changing to ActiveSheet.Range did the trick.
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Another possibility?

VBA Code:
Sub Dokat()
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim Lr As Long, lc As Long, i As Long, j As Long, arr, result
    Application.ScreenUpdating = 0
    
    Lr = Cells.Find("*", , xlFormulas, , 1, 2).Row
    lc = Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    arr = ws.Range("F2:F" & Lr)
    ReDim result(1 To UBound(arr), 1 To 1)
    
    For i = 1 To UBound(arr)
        If arr(i, 1) = "Total USA" Then result(i, 1) = 1
    Next
    
    Cells(2, lc).Resize(UBound(result)) = result
    j = WorksheetFunction.Sum(Columns(lc))
    ws.Range(ws.Cells(2, 1), ws.Cells(Lr, lc)).Sort Key1:=ws.Cells(2, lc), order1:=1, Header:=2
    If j > 0 Then ws.Range("F2").Resize(j).EntireRow.Delete
    ws.Columns(lc).ClearContents
End Sub
 
Upvote 0
Another possibility?

VBA Code:
Sub Dokat()
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim Lr As Long, lc As Long, i As Long, j As Long, arr, result
    Application.ScreenUpdating = 0
   
    Lr = Cells.Find("*", , xlFormulas, , 1, 2).Row
    lc = Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    arr = ws.Range("F2:F" & Lr)
    ReDim result(1 To UBound(arr), 1 To 1)
   
    For i = 1 To UBound(arr)
        If arr(i, 1) = "Total USA" Then result(i, 1) = 1
    Next
   
    Cells(2, lc).Resize(UBound(result)) = result
    j = WorksheetFunction.Sum(Columns(lc))
    ws.Range(ws.Cells(2, 1), ws.Cells(Lr, lc)).Sort Key1:=ws.Cells(2, lc), order1:=1, Header:=2
    If j > 0 Then ws.Range("F2").Resize(j).EntireRow.Delete
    ws.Columns(lc).ClearContents
End Sub
Thank you this one worked as well.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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