4 column sort... Fools rush in....

G

Guest

Not knowing any better, I took the recorded code for a 3 column sort and
tried to turn it into a 4 column sort. Got run time 1004 object error. Code
is below.

Is it possible to do what I'm trying this way? I'd rather not have to select
different ranges of cells and do a 3 column sort, but I may have to.
So, have I made an error in the code, or is it just not possible?
Thanks.


Sub zsortbcda()
Application.ScreenUpdating = False

Columns("A:D").Select

Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, _
Key2:=Range("C1"), Order2:=xlAscending, _
Key3:=Range("D1"), Order3:=xlAscending, _
Key4:=Range("A1"), Order4:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal, DataOption4:=xlSortNormal

Range("A1").Select

Application.ScreenUpdating = True
End Sub
 
J

JE McGimpsey

Sort only works on a maximum of three columns at a time. Do two
consecutive sorts - the first on the three least-important columns, then
the second on the most important.
 
H

Hari Prasadh

Hi Neal,

Probably you are asking for a code which could do 4 column sort in ONE
sorting operation. I dont know about that.

But programmatically you could do TWO sorting operations like the following
and get the SAME results.

Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, _
Key2:=Range("D1"), Order2:=xlAscending, _
Key3:=Range("A1"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal,

Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, _
Key2:=Range("C1"), Order2:=xlAscending, _
Key3:=Range("D1"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal,

Modify it to your requirements

Thanks a lot,
Hari
India
 
M

Myrna Larson

Did you try looking at Help for Sort? It works the same way in VBA as it does
when invoked from the Data menu.
 
N

Nigel

Hi
You can do as many sorts as you like - but only 3 at anyone time. Sorts in
Excel are persistent, so if you start with the lowest order sort key and end
with the highest order you will get the result you require.
 

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

Similar Threads


Top