Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default VBA Sort for varying dimensions

Hey everyone...

I have a spreadsheet that is the result of a previous macro I have written,
and I now in another macro I essentially want to format this sheet. The
sheet is always going to have a header in the first row and be varying in
length.

I want to sort the column in descending order by column C, ignoring the
header in the top row. My previous attempts have failed! I tried
pre-recording macro, but the sort dimensions are absolute, and I need mine to
be relative. I tried entering the range as "Range(Cells(2, 3),
Cells(last_row, 3))" where last_row is defined previously as the last used
row in the sheet.

Anyone with tips would be very much appreciated!

Thanks for the help,

Derek
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA Sort for varying dimensions

Sort has a Header parm that you can use.

And maybe you could sort the entire column:

with activesheet.range("C:C")
.Sort Key1:=.columns(1), Order1:=xlDescending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
end with

or...

Avoiding row 1 headers (data starts in row 2)

Dim myRng as range
with activesheet
set myrng = .range("C2",.cells(.rows.count,"C").end(xlup))
end with

with myrng
.Sort Key1:=.columns(1), Order1:=xlDescending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
end with

======
And you only wanted to sort that single column?????

Say you wanted to sort A:E by column C, but row 1 had headers:

Dim myRng as range
with activesheet
'lastrow determined by column A
set myrng = .range("A2:E" & .cells(.rows.count,"A").end(xlup).Row)
end with

with myrng
.Sort Key1:=.columns(3), Order1:=xlDescending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
end with

or header:=xlyes

Dim myRng as range
with activesheet
'lastrow determined by column A
set myrng = .range("A1:E" & .cells(.rows.count,"A").end(xlup).Row)
end with

with myrng
.Sort Key1:=.columns(3), Order1:=xlDescending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
end with


======
If you know your data, it's always best to specify that header parm -- don't let
excel guess.

Derek Johansen wrote:

Hey everyone...

I have a spreadsheet that is the result of a previous macro I have written,
and I now in another macro I essentially want to format this sheet. The
sheet is always going to have a header in the first row and be varying in
length.

I want to sort the column in descending order by column C, ignoring the
header in the top row. My previous attempts have failed! I tried
pre-recording macro, but the sort dimensions are absolute, and I need mine to
be relative. I tried entering the range as "Range(Cells(2, 3),
Cells(last_row, 3))" where last_row is defined previously as the last used
row in the sheet.

Anyone with tips would be very much appreciated!

Thanks for the help,

Derek


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Specify shape dimensions SteveT Excel Discussion (Misc queries) 4 April 26th 10 09:41 PM
Fix dimensions Niniel Excel Discussion (Misc queries) 15 October 11th 07 03:47 PM
Varying Data Sort Macro. MyKool Excel Programming 2 November 18th 05 04:17 PM
Three Dimensions? Charminster Developments New Users to Excel 2 November 3rd 05 04:58 PM
Sort array with 2 dimensions? eclipse Excel Programming 4 October 27th 05 04:19 PM


All times are GMT +1. The time now is 06:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"