You can use the Sort
function to sort your column by ascending or descending order.
Here is a code to order Column A
as an example:
Sub OrderColumnDescending()
Dim lastrow As Long
'If you change the column change the 1 with the number
'of your column to order
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:A" & lastrow).Sort key1:=Range("A1:A" & lastrow), _
order1:=xlDescending, Header:=xlNo
End Sub
Since you said you're new to VBA, here are some explanations about the code.
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
This line get the last used row in the Column number 1, I put the value in the lastrow
variable so I can use it later. It's better than fixed values and more readable than writting the whole thing everytime.
Range("A1:A" & lastrow).Sort key1:=Range("A1:A" & lastrow), _
order1:=xlDescending, Header:=xlNo
I create a Range object containing cells from A1 to AX where X is the last row used in column A that we calculated earlier. From this Range I call its Sort
method and give its Key1
, Order1
and Header
parameters.
Here are useful links about the Range.Sort
method.
MSDN - Range.Sort Method
Stack Overflow - VBA Excel sort range by specific column