VBA Function help - Delete entire Row as per Criteria

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to delete entire Row if a cell contain Single value, or it contain multiple values in a Column.
for that I have created Function , it work for Single Criteria. which is Very fast.

1) how to make it further dynamic for multiple Criteria.
2) if My Actual StartRow of data starts from Row 4, how to make it dynamic. Thanks for your help in advance.

Below is my attempted code, works for Single Criteria

VBA Code:
Option Explicit

Sub DeleteRow_asper_Criteria()

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Dim a As Variant
    Dim MultipleCriteria As Variant
       
     a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    
    '======Delete Single Criteria Row====
    DeleteRow ws, a, "Chris Gayle"  'Delete Single Criteria
    
    '----------Delete Multiple Criteria--------------
    MultipleCriteria = Array("MS Dhoni", "Ricky Ponting", "Brendon McCullum")
   
    
   '1)How to make  code dynamic for multiple Criteria
  ' 2) If Data startRow is 4 Row , how to make this also dynamic.
    
    
  MsgBox "Macro Successful"


End Sub


Function DeleteRow(ByVal ws As Worksheet, ByVal a As Variant, Criteria1 As String, Optional MultipleCriteria As String)
 ' Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
  Dim b As Variant
    
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) = Criteria1 Then    'How to make this line dynamic for multiple Criteria
      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)  'How to make this line dynamic if startRow changes.
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
  
End Function


Book2
ABC
1Big HittersSIXESFours
2Ricky Ponting4032553
3Sourav Ganguly3591240
4MS Dhoni4092034
5Adam Gilchrist3633597
6Sachin Tendulkar3723984
7Ricky Ponting4351258
8Chris Gayle3942206
9Virender Sehwag3981729
10Chris Gayle4823498
11Ricky Ponting3923960
12Sourav Ganguly4432829
13MS Dhoni4991688
14Adam Gilchrist4751888
15Sachin Tendulkar4312813
16Brendon McCullum4071193
17Chris Gayle2341473
18Virender Sehwag4213646
19Shane Watson4652526
20Ricky Ponting4353852
21Chris Gayle3631554
22MS Dhoni2533717
23Adam Gilchrist4583677
24Sachin Tendulkar3823754
25Brendon McCullum4241056
26Chris Gayle3171032
27Virender Sehwag2121106
28Ricky Ponting2171362
29Sourav Ganguly4561484
30MS Dhoni2252178
31Adam Gilchrist2402340
32Sachin Tendulkar4662480
33Brendon McCullum4813921
34Chris Gayle3971201
35Virender Sehwag4171079
36Shane Watson2803595
Sheet1
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Isn't this just a new variation of the same question that you asked here?
 
Upvote 0
Hi Jason,

Yes its similar to that, I was searching which method is faster for deleting rows.
I found this solution on this forum given by peter_Sss, its superb speed. It deleted 1000000 rows within 3 seconds.

this method is faster than loop, autofilter, replace approach, Therefore I want to use this method by Converting into function.




Thanks
mg
 
Upvote 0
Hi Team,

Below code is working Perfectly. for deleting multiple rows with multiple Criteria and as per table.
however is there we can shorten the code further. Thanks

VBA Code:
[CODE=vba]Option Explicit
Sub DeleteRow_asper_Criteria()

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Dim a As Variant
    Dim str As String
    Dim ary() As String
       
    a = ws.Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
   '  a = ws.Range("d5", Range("D" & Rows.Count).End(xlUp)).Value
    
    
    str = "Virender Sehwag"
    ary = Split(str, ",")
    
    'Call Function
    DeleteRow ws, a, ary, 1 'Delete Single Criteria
      
  
  MsgBox "Macro Successful"

End Sub


Function DeleteRow(ByVal ws As Worksheet, ByVal a As Variant, ByVal ary As Variant, Optional ByVal HeaderRow As Long = 1)
  Dim nc As Long, i As Long, k As Long
  Dim b As Variant, j As Long
  Dim DataRow As Long
  DataRow = HeaderRow + 1
      
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    For j = 0 To UBound(ary)
    If a(i, 1) = ary(j) Then
      b(i, 1) = 1
      k = k + 1
    End If
    Next j
  Next i

  If k > 0 Then
    Application.ScreenUpdating = False
    
    With ws.Range("A" & DataRow).Resize(UBound(a), nc) 'How to make this line dynamic if startRow changes.
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
  
End Function
[/CODE]

Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,215
Members
453,024
Latest member
Wingit77

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