Sort data based on drop-down list selection

K

Kazoo

I have 2 columns of data that i want to sort based on the
selection of a drop-down list. Only 2 selections will be
available in the drop-down list. (sort based on column A
or sort based on column B)

Any takers?



asdfzxcv
 
D

Dave Peterson

I put a dropdown from the Forms toolbar (not the control toolbox toolbar) and I
set it up to only have A and B as valid values

(right click and format control, then used a helper range with just A and B in
those two cells).

then I used this code:

Option Explicit
Sub testme()

Dim myRng As Range
Dim myDropDown As DropDown

With ActiveSheet
Set myDropDown = .DropDowns(Application.Caller)
Set myRng = .Range("a1:b" & .Cells(.Rows.Count, "A").End(xlUp).Row)
myRng.Sort key1:=.Cells(1, myDropDown.Value), _
order1:=xlAscending, header:=xlNo
End With

End Sub

and assigned it to the dropdown.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Notice I used row 1 through the last used cell in column A and I didn't include
headers.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top