• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA - sorting by user inputs

Hello,


I need some help in writing a VBA.

Detail expanation - Have two drop down lists in excel. One has all the column headers and the other has ascending or descending option. I need a VBA to sort the entire range of data depending on the inputs provided in the above mentioned two drop down lists.


Kindly help me.


Regards

Jay
 
I think this will help you get started

[pre]
Code:
Sub BasicCode()
Dim xHeader As String
Dim xSort As String
Dim MyField As Range

'Somehow these variables are assigned
xHeader = Range("A2")
xSort = Range("A3")

'Determine which field to sort
Set MyField = ActiveSheet.Range("1:1").Find(xHeader)

'which way to sort
If xSort = "Ascending" Then
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=MyField, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Else
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=MyField, _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
End If

'Apply the sort
ActiveWorkbook.ActiveSheet.Sort.Apply
End Sub
[/pre]
 
Back
Top