Pages

Monday, January 24, 2011

Sort data in vba

In this section will share the code to sort the data in ascending or descending order in excel.

For Example-

you have a data like below:-



Code:=


Sub sort_ascending()

' choose xlno if data is not having header
' change order as ascending or descending
Sheets(1).Range("a1:b" & Range("a1").End(xlDown).Row).Sort key1:=Sheets(1).Range("b:b"), order1:=xlAscending, Header:=xlYes

End Sub


Sub sort_descending()
' choose xlno if data is not having header
Sheets(1).Range("a1:b" & Range("a1").End(xlDown).Row).Sort key1:=Sheets(1).Range("b:b"), order1:=xlDescending, Header:=xlYes

End Sub

No comments:

Post a Comment