View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
p45cal[_50_] p45cal[_50_] is offline
external usenet poster
 
Posts: 107
Default Sort Without Knowing Last Cell

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

My bits incorporated (a novel name for a company perhaps?):

If you know that column G will always be fully populated right to the bottom
then:

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

If it's column E you know to be fully populated then:

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

If none of the rows is always guaranteed to be fully populated then you
might try:

lastrow=range("E3").CurrentRegion.Row+range("E3"). CurrentRegion.Rows.Count-1
Range("E3:G" & lastrow).Sort Key1:=Range("G3"), Order1:=xlDescending,
Key2:=Range("E3") _
, Order2:=xlAscending, Key3:=Range("F3"), Order3:=xlAscending
--
p45cal


"Paul Black" wrote:

Hi p45cal,

Nice play on words, I like it.
How would I incorporate your code into mine to get the optimum effect
please.
Do I need to set a variable up or something.

Thanks in Advance.
All the Best.
Paul

On Aug 31, 1:40 pm, p45cal wrote:
"turned out to be the last cell with data in which is correct."
It is correct as far as that's what I said they'd be, but it isn't ideal to
use that range in
Range("E3:G3").End(xlDown).Sort _ ...
as you're letting excel guess what range should be sorted.

With Robert's code, I think you are sorting the entire column, right to the
bottom of the sheet, fine if there's nothing below, ever. But he did check
that aspect with you.

You close with "Thanks in Advance.", does that mean you're NOT sorted?
--
p45cal



"Paul Black" wrote:
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- Hide quoted text -


- Show quoted text -