Help with looping code

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
150
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have this piece of code which finds the value "Del" in column "M" and if that value is present it deletes that row, it works perfectly but I need it to loop until there are no cells with "Del" in them. I'm just not sure how to set it up with a Do Until loop function. Any help with this would be appreciated.

VBA Code:
Private Sub BtnDel_Click()

    Sheets("Mail Merge").Activate
   
    
    iSelected = ("Del")
    lR = Cells(Rows.Count, "M").End(xlUp).Row
    Set rData = Range("M2:M" & lR).Find(iSelected, LookIn:=xlValues, lookat:=xlWhole)
    If rData Is Nothing Then: Exit Sub
        ans = rData.Row
        Rows(ans).EntireRow.Delete
   
End Sub
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,

Please use below code:

VBA Code:
Private Sub BtnDel_Click()

    Sheets("Mail Merge").Activate
       
    rowno = 2
    Do While Not (IsEmpty(Cells(rowno, "M")))
        If InStr(1, Cells(rowno, "M"), "Del") > 0 Then
            Cells(rowno, "M").EntireRow.Delete
        Else
            rowno = rowno + 1
        End If
    Loop
   
End Sub
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,974
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
You don't need to use a loop, you can just use the AutoFilter
VBA Code:
Sub Filterit()
    Application.ScreenUpdating = False
    
    With Sheets("Mail Merge").Range("M1:M" & Sheets("Mail Merge").Range("M" & Rows.Count).End(xlUp).Row)
    
        .AutoFilter 1, "Del"
        
        On Error Resume Next
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).EntireRow.Delete
        On Error GoTo 0
        .AutoFilter
    
    End With
    
    Application.ScreenUpdating = True
End Sub
 

vcoolio

Well-known Member
Joined
Jun 29, 2014
Messages
1,056
Hello TrueBlue,

Another option is to use Autofilter:-

VBA Code:
Private Sub BtnDel_Click()
  
        Dim wsMM As Worksheet: Set wsMM = Sheets("Mail Merge")
  
Application.ScreenUpdating = False

        With wsMM.[A1].CurrentRegion
                .AutoFilter 13, "Del"
                .Offset(1).EntireRow.Delete
                .AutoFilter
        End With
        
Application.ScreenUpdating = True

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Another way
VBA Code:
Private Sub BtnDel_Click()
   With Sheets("Mail merge").Range("H:H")
      .Replace "Del", True, xlWhole, , False, , False, False
      On Error Resume Next
      .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
      On Error GoTo 0
   End With
End Sub
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,974
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Hi @Fluff I think that you have made a typo with the columns and should be column M not H.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Thanks for spotting that Mark, forgot to change it back after doing a quick speed test.
As Mark has pointed out it should be
VBA Code:
Private Sub BtnDel_Click()
   With Sheets("Mail merge").Range("M:M")
      .Replace "Del", True, xlWhole, , False, , False, False
      On Error Resume Next
      .SpecialCells(xlConstants, xlLogical).EntireRow.Delete
      On Error GoTo 0
   End With
End Sub
 
Solution

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
48,400
Office Version
  1. 365
Platform
  1. Windows
.. and if your data set is large with "Del" values scattered throughout, this should be considerably faster than all the previous suggestions.

VBA Code:
Sub Delete_Del()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
  
  With Sheets("Mail Merge")
    nc = .Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    a = .Range("M2", .Range("M" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      If a(i, 1) = "Del" Then
        b(i, 1) = 1
        k = k + 1
      End If
    Next i
    If k > 0 Then
      Application.ScreenUpdating = False
      With .Range("A2").Resize(UBound(a), nc)
        .Columns(nc).Value = b
        .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
        .Resize(k).EntireRow.Delete
      End With
      Application.ScreenUpdating = True
    End If
  End With
End Sub
 

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
150
Office Version
  1. 365
Platform
  1. Windows
Thanks every one for your help. I ended up using Fluff's code. Worked a treat, thank you.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,334
Messages
5,624,087
Members
416,010
Latest member
NJT

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
Top