VBA User defined - delee Row help

Mallesh23

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

MY userdefined function works for deleting single and Multiple items as per Criteria Provided.

Now I want to reverse it , it should delete all player name excluding Below player.
str = "Sachin Tendulkar,Virender Sehwag"


Need your help to make this function Dynamic,


VBA Code:
Option Explicit
Sub DeleteRow_Excudelist_Not_Working()

    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
    
 
    str = "Sachin Tendulkar,Virender Sehwag"   'Delete both the Record, how to make opposite of it.  exclude it
     'Ary = Split(str, ",")
 
    'Call Function
    DeleteRow ws, a, str, 1 'Delete Multiple Criteria
   

  MsgBox "Macro Successful"

End Sub


Function DeleteRow(ByVal ws As Worksheet, ByVal a As Variant, ByVal str As String, 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
  Dim Ary As Variant

  Ary = Split(str, ",")

     
  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

Below is a Table

Delete Entire Row Function.xlsm
ABC
1Big HittersSIXESFours
2Sourav Ganguly3591240
3Adam Gilchrist3633597
4Sachin Tendulkar3723984
5Virender Sehwag3981729
6Sourav Ganguly4432829
7Adam Gilchrist4751888
8Sachin Tendulkar4312813
9Virender Sehwag4213646
10Shane Watson4652526
11Sachin Tendulkar4583677
12Sachin Tendulkar3823754
13Virender Sehwag2121106
14Sourav Ganguly4561484
15Sachin Tendulkar2402340
16Sachin Tendulkar4662480
17Virender Sehwag4171079
18Shane Watson2803595
19Virender Sehwag2803595
20Sourav Ganguly3591240
21Adam Gilchrist3633597
22Sachin Tendulkar3723984
23Virender Sehwag3981729
24Sourav Ganguly4432829
25Adam Gilchrist4751888
26Sachin Tendulkar4312813
27Virender Sehwag4213646
28Shane Watson4652526
29Adam Gilchrist4583677
30Sachin Tendulkar3823754
31Virender Sehwag2121106
32Sourav Ganguly4561484
33Adam Gilchrist2402340
34Sachin Tendulkar4662480
35Virender Sehwag4171079
36Virender Sehwag4171079
37Sourav Ganguly3591240
38Adam Gilchrist3633597
39Sachin Tendulkar3723984
40Virender Sehwag3981729
41Sourav Ganguly4432829
42Adam Gilchrist4751888
43Sachin Tendulkar4312813
44Virender Sehwag4213646
45Shane Watson4652526
46Adam Gilchrist4583677
47Sachin Tendulkar3823754
48Virender Sehwag2121106
49Sourav Ganguly4561484
50Adam Gilchrist2402340
51Sachin Tendulkar4662480
52Virender Sehwag4171079
53Shane Watson2803595
54Shane Watson2803595
55Sourav Ganguly3591240
56Adam Gilchrist3633597
57Sachin Tendulkar3723984
58Virender Sehwag3981729
59Sourav Ganguly4432829
60Adam Gilchrist4751888
61Sachin Tendulkar4312813
62Virender Sehwag4213646
63Shane Watson4652526
64Adam Gilchrist4583677
65Sachin Tendulkar3823754
66Virender Sehwag2121106
67Sourav Ganguly4561484
68Adam Gilchrist2402340
69Sachin Tendulkar4662480
70Virender Sehwag4171079
71Shane Watson2803595
Sheet2



Below is dummy expected output
Delete Entire Row Function.xlsm
EFG
1Big HittersSIXESFours
4Sachin Tendulkar4312813
5Virender Sehwag4213646
8Virender Sehwag2121106
9Sachin Tendulkar4583677
Sheet2


Thanks
mg
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
VBA Code:
Function DeleteRow(ByVal ws As Worksheet, ByVal a As Variant, ByVal str As String, 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
  Dim Ary As Variant

  Ary = Split(str, ",")

      
  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 IsError(Application.Match(a(i, 1), Ary, 0)) Then
          b(i, 1) = 1
          k = k + 1
      End If
  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
 
Upvote 0
Hi Fluff,

Great ! it worked as expected, how to make further dynamic,

Function to delete include list... If provided include list.
Function to delete All Exclude list... If provided Exclude list. both in a single function.

Thanks in advance!



Thanks
mg
 
Upvote 0
How about
VBA Code:
Function DeleteRow(ByVal ws As Worksheet, ByVal a As Variant, ByVal str As String, Optional ByVal HeaderRow As Long = 1, Optional Inc_Exc As Boolean = True)
  Dim nc As Long, i As Long, k As Long
  Dim b As Variant, j As Long
  Dim DataRow As Long
  DataRow = HeaderRow + 1
  Dim Ary As Variant

  Ary = Split(str, ",")

      
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  ReDim b(1 To UBound(a), 1 To 1)
  If Inc_Exc Then
      For i = 1 To UBound(a)
          If Not (IsError(Application.Match(a(i, 1), Ary, 0))) Then
              b(i, 1) = 1
              k = k + 1
          End If
      Next i
  Else
      For i = 1 To UBound(a)
          If IsError(Application.Match(a(i, 1), Ary, 0)) Then
              b(i, 1) = 1
              k = k + 1
          End If
      Next i
  End If
  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
 
Upvote 0
Hi Fluff,

Thats Perfect !! Thanks again for your help , it worked ! ?


Thanks
mg
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,060
Latest member
mtsheetz

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