PDA

View Full Version : Solved: Sort Non-Adjacent Columns



zoom38
06-07-2006, 06:22 PM
Can a sort be done on non-adjacent columns? I have column A and column AE that I need sorted by key1=AE and key2=A, can this be done?

Thanks
Gary

mvidas
06-08-2006, 05:29 AM
Hi Gary,

Are you saying that you want to sort only those columns, leaving B:AD as they are?
You can do that by moving them next to each other beforehand:Sub dlfkjdf()
Application.ScreenUpdating = True
With ActiveSheet
.Columns("AE").Cut
.Columns("B").Insert
.Range("A:B").Sort Key1:=.Columns("B"), Key2:=.Columns("A"), Header:=xlYes
.Columns("B").Cut
.Columns("AF").Insert
End With
Application.ScreenUpdating = True
End SubMatt

zoom38
06-08-2006, 10:40 AM
Matt that is exactly what i am saying. I was going to just move column AE to Column A but then I would have to hide it which would cause havoc on my code. (Don't know how to account for for hidden columns in my code). I am a novice Excel and VBA user. I will give your code a try.

Thanks
Gary

zoom38
06-08-2006, 12:10 PM
Matt this is what I ended up with within my sorting sub and so far so good.



'Move Column AF to Column B to Sort, Then Move It Back To Its Original Place
With ActiveSheet
.Columns("AF").Cut
.Columns("B").Insert
.Range("A5:B" & LastRow).Sort Key1:=Range("B5"), Key2:=Range("A5"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Columns("B").Cut
.Columns("AG").Insert
End With


Thanks Again
Gary