Sort from Largest to Smallest - vba

I need to sort the table from Largest to Smallest as column Q.
codes gives no errors but doesnt work as well. It firstly filters well and sorts with no success:
introws = Range("A1").End(xlDown).Row
ActiveSheet.Range("$A$1:$q$" & introws).AutoFilter Field:=17, Criteria1:=">4", Operator:=xlFilterValues
Range("$A$1:$q$" & introws).Sort Key1:=Range("q:q"), Order1:=xlDescending
Range("q:q").Sort (xlDescending)
Both lines dont work, it just stays unsorted....
How to write this?

To sort a range value with filter, you need to apply specific method in a sequence, it involve clear sortfields > add key > sort.apply, the following is a simple sort for the range with data filter:
With Sheet1
.AutoFilter.Sort.SortFields.Clear
.AutoFilter.Sort.SortFields.Add2 Key:= _
Range("B1"), SortOn:=xlSortOnValues, Order:=xlDescending
.AutoFilter.Sort.Apply
End With
To apply the sort method in your case, you may try to following code modication, it should be work as expected, please perform a test for both case:
introws = Range("A1").End(xlDown).Row
ActiveSheet.Range("$A$1:$q$" & introws).AutoFilter Field:=17, Criteria1:=">4", Operator:=xlFilterValues
With ActiveSheet
.AutoFilter.Sort.SortFields.Clear
.AutoFilter.Sort.SortFields.Add2 Key:= _
Range("Q1"), SortOn:=xlSortOnValues, Order:=xlDescending
.AutoFilter.Sort.Apply
End With

Related

excel vba: Can someone help me understand how to use a cusom sort order?

I've been working on a code to sort a block of data using Range.Sort using a custom sort order. I've tried to record macros and look online but came up more confused about this problem.
For the key1:= argument; is it a single cell (e.g. Range("A1")) or a whole column?
How exactly can I use a custom sort order in OrderCustom:=?
In the case I'm going about this all wrong; the Range in Range.Sort can be anywhere as long as its in one continuous block, correct?
Here is the code I'm working with:
Sub Test()
Dim quantity As Variant
quantity = 2 + WorkshetFunction.Count(Range("A" & 3, "K" & 900))
With Range("A" & 3, "K" & quantity)
.Sort key1:=Range("A" & 3)
Order:=xlAscending
Header:=xlNo
OrderCustom:="VALID, GOOD, DUE, OVERDUE, WAY OVERDUE, MISSING"
'> This is the order in which I want the items on this list sorted by.
End With
End Sub
The Range("A" & 3, "K" & quantity) refers to a block of data containing on "A" the "status" of some items determined by their calibration expiration dates, amongst other data which is irrelevant for this purpose, and I'm not in the liberty to share. All I'm asking is help understanding the inner workings of the .Sort method. Thanks!
Give'r
Sub SortItOut()
Dim rng As Range, sh As Worksheet
Set sh = Sheets("Sheet1")
With sh
Set rng = .Range("A3:K" & .Cells(.Rows.Count, "K").End(xlUp).Row)
With rng
sh.Sort.SortFields.Clear
sh.Sort.SortFields.Add Key:=Range("A3") _
, SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"VALID,GOOD,DUE,OVERDUE,WAY OVERDUE,MISSING", DataOption:=xlSortNormal
End With
With .Sort
.SetRange rng
.Orientation = xlTopToBottom
.Apply
End With
End With
End Sub

How to sort a column from newest to oldest?

I have the following VBA code:
Sub sort()
Range("M2:M").Sort _
Key1:=Range("M2"), Order1:=xlAscending
End Sub
But, it's not working. How do I fix it?
It's not working because you are using a google-sheets syntax on the column; excel wants a last row specified. The Sort also want the primary key to be the first cell in the "M2:M" & lr range. Telling it M2, relative to Range("M2:M" & lr) is actually referencing Y3. You should know if you are using a header or not; I'm assuming any header label is in M1 so for Range("M2:M" & lr) you use Header:=xlNo.
Sub msort()
dim lr as long
lr = cells(rows.count, "M").end(xlup).row
with Range("M2:M" & lr)
.Sort Key1:=.cells(1), Order1:=xldescending, _
Orientation:=xlTopToBottom, Header:=xlNo
end with
End Sub
You should also avoid reserved words for the names of your sub procedures and variables. Get into the habit of providing parent worksheet references.
btw, dates ordered newest to oldest are in an xldescending order, not xlascending.

Referencing multiple cells VBA Autofilter

I am using the following code to apply an autofilter to a range and filter on one of the columns by looking for the same value that is contained in several reference cells:
Sub filter()
Range("B6:N9000").AutoFilter Field:=2, Criteria1:=Array(Range("C2").Value, Range("D2").Value, Range("E2").Value )
End Sub
The problem however is the filter only applies the LAST cell referenced in the code, ie for above it ONLY looks up "E2", not "C2" & "D2" & "E2"
Any suggestions? Thanks
To put an answer under this: You want to add the argument Operator:=xlFilterValues to your call, so it will look like this:
Range("B6:N9000").AutoFilter Field:=2, Criteria1:=Array(Range("C2").Value, _
Range("D2").Value, Range("E2").Value), Operator:=xlFilterValues
or
[B6:N9000].AutoFilter Field:=2, _
Criteria1:=Array([C2].Value, [D2].Value, [E2].Value), Operator:=xlFilterValues

Custom sorting with custom ordering

I want to sort my rows based on the values in column G. There are 3 possible values: Green, Red and Yellow. I want the rows sorted with Green on top, then Yellow, then Red.
Everything I try results in the sort order being alphabetical: Green, Red then Yellow. There is a secondary sort on column R, but that is working fine.
My latest code is below. rr is the number of the last row. I have tried it with and without Order1:=xlAscending.
sCustomList = "Green" "Yellow" "Red"
Application.AddCustomList ListArray:=sCustomList
Range("A3:T" & rr).Sort Key1:=Range("G3:G" & rr), Order1:=xlAscending, _
OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
DataOption1:=xlSortNormal, Key2:=Range("R3:R" & rr), Order2:=xlAscending
Looking at your code, the sCustomList looks like a string type variable and not a variant array. My success with custom sort lists has been to create a new one every time and use the highest index number to reference it.
Sub custom_sort()
Dim vCustom_Sort As Variant, rr As Long
vCustom_Sort = Array("green", "yellow", "red", Chr(42))
Application.AddCustomList ListArray:=vCustom_Sort
With Worksheets("Sheet2") '<~~ set this properly!
.Sort.SortFields.Clear
rr = .Cells(Rows.Count, "G").End(xlUp).Row
With .Range("A3:T" & rr)
'use this to presort one or more secondary fields before the primary custom sort
'.Cells.Sort Key1:=.Columns(18), Order1:=xlAscending, _
Key2:=.Columns(1), Order2:=xlDescending, _
Orientation:=xlTopToBottom, Header:=xlYes
.Cells.Sort Key1:=.Columns(7), Order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes, MatchCase:=False, _
OrderCustom:=Application.CustomListCount + 1
End With
.Sort.SortFields.Clear
End With
End Sub
There is a twist between .Cells.Sort.SortFields.Add and .Cells.Sort that usually generates some confusion. The .SortFields.Add method uses a CustomOrder:= parameter and the Range.Sort method uses a OrderCustom:= parameter. The two are most definitely NOT the same but often get used interchangeably with disastrous results.
If you use the SortFields object, you don't have to refer to custom lists:
It should be obvious below where to change the various range references. I also added an alphabetical sort on one of the columns other than G
Option Explicit
Sub TrafficLightSort()
Dim WS As Worksheet
Dim rSortRange As Range, rSortKey As Range
Const sSortOrder As String = "Green,Yellow,Red"
Set WS = Worksheets("sheet1")
With WS
Set rSortRange = Range("E1", .Cells(.Rows.Count, "E").End(xlUp)).Resize(columnsize:=3)
Set rSortKey = rSortRange.Columns(3)
With .Sort.SortFields
.Clear
.Add Key:=rSortKey, _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
CustomOrder:=sSortOrder
.Add Key:=rSortRange.Columns(1), _
SortOn:=xlSortOnValues, _
Order:=xlAscending
End With
With .Sort
.SetRange rSortRange
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End With
End Sub
I would recommend adding a table with an explicit sorting order to your worksheet (green = 1, yellow = 2, etc). Then add a column to your sorting range that uses a lookup function to return the sorting value and use VBA to run a standard sort based on that column. This will leave you with VBA code that is easier for developers to read, allows Excel users who don't read VBA to see your sort order, and avoids burying hardcoded values deep in you macro.

Filter to exclude formula and blank

Filter field has many numbers, formula (result is "-") and blank.
How to write a VBA code to filter all numbers and exclude "-" and blank.
"-" is not text or string it's the result of a formula.
On Error Resume Next
ActiveSheet.ShowAllData
Range("G8").AutoFilter Filed:=7, Criteria2:="="
Range("N8").AutoFilter Field:=14, Criteria1:="<>-", _
Operator:=xlAnd, Criteria2:="<>"
Try this:
Edit1: For your example, it should be:
Range("N8").AutoFilter Field:=1, Criteria1:="<>-" _
, Criteria2:="<>", Operator:=xlAnd
This will filter out blanks and cells with - as a result of formula.
Take note that you're only working one Cell N8 which only have 1 field of data.
Edit2: Another way to make it work is to explicitly define the range you're working on.
Dim r As Range
Set r = Sheets("Sheet1").Range("A1:N100") 'change to suit
r.AutoFilter Field:=14, Criteria1:="<>-" _
, Criteria2:="<>", Operator:=xlAnd
Is this what you're trying? HTH.