View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Mahi Mahi is offline
external usenet poster
 
Posts: 6
Default Sorting multiple columns in VBA

I'd like to sort multiple columns in VBA (Excel 2003). That's not a
problem you'd think, but the columns are not placed next to each other.
There are columns in between which may NOT be sorted!

Since a simple example says more than words... Take following table:

[A] B [C] D [E]
+-------+-----------+-------+---------+-------+
1 | B | =IF(A1... | 12| =C1*... | 75|
2 | D | =IF(A2... | 11| =C2*... | 70|
3 | A | =IF(A3... | 19| =C3*... | 80|
4 | C | =IF(A4... | 20| =C4*... | 40|

We want to sort columns A, C and E alphabetically according to the data
in column A, but leave B and D alone. This should lead to:

[A] B [C] D [E]
+-------+-----------+-------+---------+-------+
1 | A | =IF(A1... | 19| =C1*... | 80|
2 | B | =IF(A2... | 12| =C2*... | 75|
3 | C | =IF(A3... | 20| =C3*... | 40|
4 | D | =IF(A4... | 11| =C4*... | 70|

Selecting columns A, C and E without selecting B and D isn't a problem:

Range("A1:A4,C1:C4,E1:C4").Select

But when I try a sorting construction like shown below, I get an error:

Range("A1:A4,C1:C4,E1:C4").Sort Key1:Range("A1"), Order1:xlAscending

I'm not sure why though... Anyone knows a way to accomplish what I want?

--
mahi