View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Paul Black Paul Black is offline
external usenet poster
 
Posts: 394
Default Sort Without Knowing Last Cell

Hi p45cal,

The selected cells as per your example for both the examples I gave
turned out to be the last cell with data in which is correct.

Hi Robert McCurdy,

My ranges do have headers in and I have adjusted my code accordingly,
thank you.

Thanks in Advance.
All the Best.
Paul

On Aug 31, 11:42 am, p45cal wrote:
"... are you saying that this is NOT the optimum way to do it?. "

No. I didn't say that.. but it isn't.
Range("E3:G3").End(xlDown)
is only one cell at the bottom of contiguous data in column E, you are
probably leaving it up to Excel to guess the range from there.

To show this, try this statement and look what's selected:
Range("E3:G3").End(xlDown).select

--
p45cal



"Paul Black" wrote:
Thanks for the reply p45cal,


I had just worked out doing it this way ...


Range("E3:G3").End(xlDown).Sort _
Key1:=Range("G3"), Order1:=xlDescending, _
Key2:=Range("E3"), Order2:=xlAscending, _
Key3:=Range("F3"), Order3:=xlAscending


.... and ...


Range("I3:L3").End(xlDown).Sort _
Key1:=Range("L3"), Order1:=xlDescending, _
Key2:=Range("I3"), Order2:=xlAscending, _
Key3:=Range("J3"), Order3:=xlAscending


.... are you saying that this is NOT the optimum way to do it?.


Thanks in Advance.
All the Best.
Paul


On Aug 31, 11:19 am, "Robert McCurdy" wrote:
A few questions:
Does your table have headers in row 2?
Is your table clear of internal blank rows?
Are there other tables or data below your desired sortable range/table?


Assuming the answers are, Yes, Yes, No (as it should), then:


Sub Dsort()
Dim SortRng As Range
Set SortRng = Range("E2:G" & Rows.Count)
SortRng.Sort Key1:=Range("G2"), Order1:=xlDescending, Header:=xlYes, _
Key2:=Range("E2"), Order2:=xlAscending, Header:=xlYes, _
Key3:=Range("F2"), Order3:=xlAscending, Header:=xlYes
End Sub


Regards
Robert McCurdy"Paul Black" wrote in ooglegroups.com...


Hi everyone,


I have a program that outputs columns of values to a worksheet.
I have titles in row 2 and the values start in columns "E3:G?",
"I3:L?" etc.
I would like to sort the values but I don't know what the last row
will be.
Here is an example for columns "E3:G?" ...


Range("E3:G?").Select
Selection.Sort Key1:=Range("G3"), Order1:=xlDescending,
Key2:=Range("E3") _
, Order2:=xlAscending, Key3:=Range("F3"), Order3:=xlAscending


... with the ? is the unknown number to sort down to.


Thanks in Advance.
All the Best.
Paul- Hide quoted text -


- Show quoted text -