Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excel VBA: Filter and delete rows based on an array criteria range

I have the following code and am struggling to get it to work.

Sub FilterAndRemove()
        
    Dim varDelItem As Variant
    Dim lngRowStart As Long, _
        lngRowLast As Long, _
        lngRowActive As Long
    Dim strMyCol As String
    Dim rngDelRange As Range
    
    varDelItem = Application.Transpose(Worksheets("ELEMENTS").Range("AA2:AA32"))
    lngRowStart = 2
    strMyCol = "A"
    lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row
    
    Application.ScreenUpdating = False
        
    For lngRowActive = lngRowStart To lngRowLast
        If Cells(lngRowActive, strMyCol) <> varDelItem Then
            If rngDelRange Is Nothing Then
                Set rngDelRange = Cells(lngRowActive, strMyCol)
            Else
                Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
            End If
        End If
    Next lngRowActive
        
    If Not rngDelRange Is Nothing Then
        rngDelRange.EntireRow.Delete xlShiftUp
    End If
    
    Application.ScreenUpdating = True
        
End Sub

What I am trying to achieve: I have a list of values which will be the conditions. After pasting data onto active sheet and activating the macro, the filter function should find rows not containing the data from the conditions list and remove them.

I can get it to work by specifying only one condition, i.e. Range("AA2"), but I want it to check against the whole range/array. What is wrong with my code? I am getting Type Mistmatch error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance.

>Solution :

Pls. try this code

Sub comp()
    Dim varDelItem As Variant
    Dim lngRowStart As Long, _
        lngRowLast As Long, _
        lngRowActive As Long
    Dim strMyCol As String
    Dim rngDelRange As Range
    varDelItem = Application.Transpose(Worksheets("ELEMENTS").Range("AA2:AA32"))
    lngRowStart = 2
    strMyCol = "A"
    lngRowLast = Cells(rows.Count, strMyCol).End(xlUp).Row
    
    
    Application.ScreenUpdating = False
        

    For lngRowActive = lngRowStart To lngRowLast
        ident = False     'added
        For Each cell In varDelItem    'added
            If Cells(lngRowActive, strMyCol) = cell Then ident = True: Exit For   'added
        Next cell       'added
        If Not ident Then  'changed
            If rngDelRange Is Nothing Then
                Set rngDelRange = Cells(lngRowActive, strMyCol)
            Else
                Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
            End If
        End If
    Next lngRowActive

   If Not rngDelRange Is Nothing Then
        rngDelRange.EntireRow.Delete xlShiftUp
    End If
    
    Application.ScreenUpdating = True
        
End Sub

The ident variable is True if a match is found in the conditions range. Then not execute the adding to the deletion range.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading