VBA - delete row if cell contains

sherwood16

New Member
Joined
Mar 27, 2012
Messages
24
Hi all,

Trying to search this but having no luck.

I'm after some VBA where I can delete the whole row of a table, if column C contains the word "Charge"?

Can someone help?

Thanks.
 
Welcome to the forum,

This will delete each row in column C where it meets your criteria.

Sub myDeleteRows()
Dim MyCol As String
Dim i As Integer
For i = 1 To Range("C" & "65536").End(xlUp).Row Step 1
If Application.WorksheetFunction.CountIf(Range("A" & i & ":AZ" & i), "Charge") > 0 Then
Range("C" & i).EntireRow.Delete
End If
Next i

End Sub
 
Upvote 0
This is really helpful! Was wondering however if there is a way to update the code in order to apply this to the whole worksheet?

At the moment if I have say, 2 rows where the 'Delete' criteria has been met I have to run the maco twice (once to remove each row).

Any advice much appreciated!
 
Upvote 0
Reverse order of For i = LastRow To 1 Step -1 can help.
For example try this:
Rich (BB code):

' Deleting entire rows with MyTarget
Sub myDeleteRows1()
  
  Const MyTarget = "Charge" ' <-- change to suit
  
  Dim Rng As Range
  Dim i As Long, j As Long
  
  ' Calc last row number
  j = Cells.SpecialCells(xlCellTypeLastCell).Row  'can be: j = Range("C" & Rows.Count).End(xlUp).Row
  
  ' Collect rows with MyTarget
  For i = j To 1 Step -1
    If WorksheetFunction.CountIf(Rows(i), MyTarget) > 0 Then
      If Rng Is Nothing Then
        Set Rng = Rows(i)
      Else
        Set Rng = Union(Rng, Rows(i))
      End If
    End If
  Next
  
  ' Delete rows with MyTarget
  If Not Rng Is Nothing Then Rng.Delete
  
  ' Update UsedRange
  With ActiveSheet.UsedRange: End With
  
End Sub
 
Upvote 0
More safe & fast version:
Rich (BB code):

' Deleting entire rows with MyTarget
Sub myDeleteRows2()
  
  Const MyTarget = "Charge" ' <-- change to suit
  
  Dim Rng As Range, DelCol As New Collection, x
  Dim i As Long, j As Long, k As Long
  
  ' Calc last row number
  j = Cells.SpecialCells(xlCellTypeLastCell).Row  'can be: j = Range("C" & Rows.Count).End(xlUp).Row
  
  ' Collect rows range with MyTarget
  For i = 1 To j
    If WorksheetFunction.CountIf(Rows(i), MyTarget) > 0 Then
      k = k + 1
      If k = 1 Then
        Set Rng = Rows(i)
      Else
        Set Rng = Union(Rng, Rows(i))
        If k >= 100 Then
          DelCol.Add Rng
          k = 0
        End If
      End If
    End If
  Next
  If k > 0 Then DelCol.Add Rng
  
  ' Turn off screen updating and events
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  
  ' Delete rows with MyTarget
  For Each x In DelCol
    x.Delete
  Next
  
  ' Update UsedRange
  With ActiveSheet.UsedRange: End With
  
  ' Restore screen updating and events
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  
End Sub
 
Last edited:
Upvote 0

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