Filter by two criteria

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I have been using this code to sort out a worksheet and pass the results to another sheet, and it works fine. But I was thinking of using this to sort another WS, except that I need to sort this one by two criteria's: column 7 is blank, but column 5 would not be blank. I am unsure of how to tweak the code to add in the second sorting action, all of the ways I tried have not worked. Is it possible to sort by two criteria's?

I appreciate any input - thanks,


VBA Code:
Dim rng As Range    
    With Sheets("Repair Log")
        Set rng = .Range("A1:G" & .Range("A" & Rows.Count).End(xlUp).Row)
    End With
    With rng
        .AutoFilter Field:=7, Criteria1:=""
        .SpecialCells(xlCellTypeVisible).Copy
        Sheets("Repair Report").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
        Sheets("Repair Report").Cells.EntireColumn.AutoFit
    End With
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
15,488
Office Version
  1. 365
Platform
  1. Windows
I am unsure of how to tweak the code to add in the second sorting action, all of the ways I tried have not worked. Is it possible to sort by two criteria's?
You can manually filter by 2 criteria which means that you can do it by code as well. If you try filtering manually with the macro recorder running then it will give you the base code that you need.
 
Upvote 0

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
85,187
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Dim rng As Range   
    With Sheets("Repair Log")
        Set rng = .Range("A1:G" & .Range("A" & Rows.Count).End(xlUp).Row)
    End With
    With rng
        .AutoFilter Field:=7, Criteria1:=""
        .AutoFilter Field:=5, Criteria1:="<>"
        .SpecialCells(xlCellTypeVisible).Copy
        Sheets("Repair Report").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
        Sheets("Repair Report").Cells.EntireColumn.AutoFit
    End With

PS, this filtering, not sorting. ;)
 
Upvote 0
Solution

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
How about
VBA Code:
Dim rng As Range  
    With Sheets("Repair Log")
        Set rng = .Range("A1:G" & .Range("A" & Rows.Count).End(xlUp).Row)
    End With
    With rng
        .AutoFilter Field:=7, Criteria1:=""
        .AutoFilter Field:=5, Criteria1:="<>"
        .SpecialCells(xlCellTypeVisible).Copy
        Sheets("Repair Report").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
        Sheets("Repair Report").Cells.EntireColumn.AutoFit
    End With

PS, this filtering, not sorting. ;)
(doh! yes, filtering - thinking that, but not saying that. lol)

Thanks Fluff - I was so close to getting it right without having to ask ;) That worked perfectly, I really appreciate the assistance again.

Here's what I tried...

.AutoFilter Field:=7, Criteria1:="", .AutoFilter Field:=5, Criteria1:<>""

.AutoFilter Field:=7, Criteria1:="",
.AutoFilter Field:=5, Criteria1:<>""
and a few others...
 
Upvote 0

Fluff

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

Forum statistics

Threads
1,196,006
Messages
6,012,824
Members
441,731
Latest member
jonceramic

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