PDA

View Full Version : Solved: sort specific columns



av8tordude
11-12-2012, 01:12 AM
Is it possible to sort specific columns? For example sort columns A:C & F:H but not sort D & E

I have this code, but it sorts D & E also, which I don't want sorted.

Range("A1:C10", "F1:H10").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo

p45cal
11-12-2012, 02:06 AM
I don't think so.
As a work-around and only if the values in columns D and E are not formulae, try:x = Range("D1:E10").Value
Range("A1:H10").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
Range("D1:E10").Value = x
By the way, note that
Range("A1:C10", "F1:H10")
is actually the same as
Range("A1:H10")
you were probably looking for:
Range("A1:C10, F1:H10")

av8tordude
11-12-2012, 02:28 AM
Thanks P45Cal,

For learning purposes, can you offer some insight into what is happening in the code you provided. Thx.

av8tordude
11-12-2012, 02:49 AM
On other thing, if I have Option Explicit, I receive an error stating "Variable not define"

p45cal
11-12-2012, 03:24 AM
For learning purposes, can you offer some insight into what is happening in the code you provided.x stores the values in columns D and E in an array before sorting.
The sort then takes place on the entire range (columns A to H), including D and E
Then the just-sorted values in D and E columns have their values overwritten by the old values held in x.

Dim x
somewhere early in the sub to prevent variable not defined error.

av8tordude
11-12-2012, 04:56 AM
Thank you p45cal...:)