Using autofilters to delete rows with 2 criteria.

siaoz19

New Member
Joined
Jul 27, 2011
Messages
7
I have a set of datas all in a column A:A.
i would only like to have the values with n and ( at the start of the value to appear after filtering.

Currently my formula looks like this.

Sub Delete_with_Autofilter()
Dim DeleteValue As String
Dim rng As Range
Dim calcmode As Long

With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'Fill in the value that you want to delete
'Tip: use DeleteValue = "<>ron" to delete rows without ron
DeleteValue = "<>n*"
DeleteValue = "<>(*"

'Sheet with the data, you can also use Sheets("MySheet")
With ActiveSheet

'Firstly, remove the AutoFilter
.AutoFilterMode = False

'Apply the filter
.Range("A1:A" & .Rows.Count).AutoFilter Field:=1, Criteria1:=DeleteValue

With .AutoFilter.Range
On Error Resume Next
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then rng.EntireRow.Delete
End With

'Remove the AutoFilter
.AutoFilterMode = False
End With

With Application
.ScreenUpdating = True
.Calculation = calcmode
End With

End Sub</pre>the result i ended up with was only data starting with brackets.

How do i have 2 criteria not filtering each other out?
 
Assuming
Sheet name = Sheet1 (can adjust)
Column A: Header in row 1, data beginning in row 2 (can adjust)

The code below (based in Ron de Bruin Loop_Example) deletes rows whose cells in column A doesnt start with n, N, t, T or (

(try it on a test-workbook)

Code:
Sub Loop_Example()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
 
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
 
    'Adjust this to your sheet name
    With Sheets("Sheet1")
 
        'We select the sheet so we can change the window view
        .Select
 
        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
 
        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False
 
        'Set the first and last row to loop through
        Firstrow = 2
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
 
        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1
 
            'We check the values in the A column in this example
            With .Cells(Lrow, "A")
 
                If Not IsError(.Value) Then
                    If UCase(Left(.Value, 1)) <> "N" And _
                    Left(.Value, 1) <> "(" And _
                    UCase(Left(.Value, 1)) <> "T" Then .EntireRow.Delete
                End If
 
            End With
 
        Next Lrow
    End With
 
End Sub

HTH

M.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Sorry, when i pasted the macro something wrong has happened

Missed this part
ActiveWindow.View = ViewMode

With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With




This is correct

Code:
Sub Loop_Example()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
 
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
 
    'Adjust this to your sheet name
    With Sheets("Sheet1")
        'We select the sheet so we can change the window view
        .Select
        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False
        'Set the first and last row to loop through
        Firstrow = 2
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
 
        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1
            'We check the values in the A column in this example
            With .Cells(Lrow, "A")
                If Not IsError(.Value) Then
                    If UCase(Left(.Value, 1)) <> "N" And _
                    Left(.Value, 1) <> "(" And _
                    UCase(Left(.Value, 1)) <> "T" Then .EntireRow.Delete
                End If
            End With
        Next Lrow
    End With
 
    ActiveWindow.View = ViewMode
 
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
 
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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