#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default sorting range

hi,
I'm using the following code to sort a selected range based on the columns,
C,B and F.I don't know why the range is sorted only based on Column C?
Sub Rectangle1954_Click()
Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort key1:=Columns("C"), order1:=xlAscending, header:=xlNo,
MatchCase:=False, Orientation:=xlTopToBottom, key2:=Columns("B"),
order1:=xlAscending, header:=xlNo, MatchCase:=False,
Orientation:=xlTopToBottom, key3:=Columns("F"), order1:=xlAscending,
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

any help?
thanx
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default sorting range

The code wil onluy sort the selected area. try this test code and see what
is selected after it runs

sub test()

Areas(1).select

end sub

The area that is selected is the area that is sorted in you code.
"peyman" wrote:

hi,
I'm using the following code to sort a selected range based on the columns,
C,B and F.I don't know why the range is sorted only based on Column C?
Sub Rectangle1954_Click()
Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort key1:=Columns("C"), order1:=xlAscending, header:=xlNo,
MatchCase:=False, Orientation:=xlTopToBottom, key2:=Columns("B"),
order1:=xlAscending, header:=xlNo, MatchCase:=False,
Orientation:=xlTopToBottom, key3:=Columns("F"), order1:=xlAscending,
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

any help?
thanx

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default sorting range

it's fine.I have no problem in Area that's selected.I need to know why it
only sorts the first "key" no matter which one is first.

"Joel" wrote:

The code wil onluy sort the selected area. try this test code and see what
is selected after it runs

sub test()

Areas(1).select

end sub

The area that is selected is the area that is sorted in you code.
"peyman" wrote:

hi,
I'm using the following code to sort a selected range based on the columns,
C,B and F.I don't know why the range is sorted only based on Column C?
Sub Rectangle1954_Click()
Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort key1:=Columns("C"), order1:=xlAscending, header:=xlNo,
MatchCase:=False, Orientation:=xlTopToBottom, key2:=Columns("B"),
order1:=xlAscending, header:=xlNo, MatchCase:=False,
Orientation:=xlTopToBottom, key3:=Columns("F"), order1:=xlAscending,
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

any help?
thanx

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default sorting range

Your sorting by column C of the activesheet--not the 3rd column of the first
area.

Maybe...

With myRng
.Sort key1:=.Columns(3), order1:=xlAscending, _
key2:=,Columns(2), order2:=xlAscending, _
key3:=.Columns(6), order3:=xlAscending, _
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With

You may want to check to see if that Area has at least 6 columns, too:

if myrng.columns.count < 6 then
'error message or resize or ????



peyman wrote:

hi,
I'm using the following code to sort a selected range based on the columns,
C,B and F.I don't know why the range is sorted only based on Column C?
Sub Rectangle1954_Click()
Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort key1:=Columns("C"), order1:=xlAscending, header:=xlNo,
MatchCase:=False, Orientation:=xlTopToBottom, key2:=Columns("B"),
order1:=xlAscending, header:=xlNo, MatchCase:=False,
Orientation:=xlTopToBottom, key3:=Columns("F"), order1:=xlAscending,
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

any help?
thanx


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default sorting range

There are quite a number of problems with your code. First of all, I wonder
if you really mean Selection.CurrentRegion rather than Selection.Areas(1),
but that's up to you and your data. Next, the parameters to the Sort method
are all screwed up. Simplify to the code below. You'll find that the code is
much easier to read and maintain if you split long lines into single lines,
using a '_' character as a line continuation character. I tend to split
lines to one parameter per line.

Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort _
key1:=Columns("C"), _
order1:=xlAscending, _
key2:=Columns("B"), _
order2:=xlAscending, _
key3:=Columns("F"), _
order3:=xlAscending, _
header:=xlNo, _
MatchCase:=False, _
Orientation:=xlTopToBottom
End With

You had multiple arguments for Header, Orientation, MatchCase and Order1,
while at the same time not having Order2 or Order3. I'm actually surprised
your code even compiled with duplicated named arguments. Finally, you need
to ensure that the selected area contains columns B, C and F. If you select
a single cell within the range to be sorted, only that one cell will be
sorted, which basically means nothing happens.

Cleaning up the code in to a nice indented readable form makes many error
very easy to find.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"peyman" wrote in message
...
hi,
I'm using the following code to sort a selected range based on the
columns,
C,B and F.I don't know why the range is sorted only based on Column C?
Sub Rectangle1954_Click()
Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort key1:=Columns("C"), order1:=xlAscending, header:=xlNo,
MatchCase:=False, Orientation:=xlTopToBottom, key2:=Columns("B"),
order1:=xlAscending, header:=xlNo, MatchCase:=False,
Orientation:=xlTopToBottom, key3:=Columns("F"), order1:=xlAscending,
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

any help?
thanx




  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 189
Default sorting range

thanx.got it.

"Chip Pearson" wrote:

There are quite a number of problems with your code. First of all, I wonder
if you really mean Selection.CurrentRegion rather than Selection.Areas(1),
but that's up to you and your data. Next, the parameters to the Sort method
are all screwed up. Simplify to the code below. You'll find that the code is
much easier to read and maintain if you split long lines into single lines,
using a '_' character as a line continuation character. I tend to split
lines to one parameter per line.

Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort _
key1:=Columns("C"), _
order1:=xlAscending, _
key2:=Columns("B"), _
order2:=xlAscending, _
key3:=Columns("F"), _
order3:=xlAscending, _
header:=xlNo, _
MatchCase:=False, _
Orientation:=xlTopToBottom
End With

You had multiple arguments for Header, Orientation, MatchCase and Order1,
while at the same time not having Order2 or Order3. I'm actually surprised
your code even compiled with duplicated named arguments. Finally, you need
to ensure that the selected area contains columns B, C and F. If you select
a single cell within the range to be sorted, only that one cell will be
sorted, which basically means nothing happens.

Cleaning up the code in to a nice indented readable form makes many error
very easy to find.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"peyman" wrote in message
...
hi,
I'm using the following code to sort a selected range based on the
columns,
C,B and F.I don't know why the range is sorted only based on Column C?
Sub Rectangle1954_Click()
Dim myRng As Range
Set myRng = Selection.Areas(1)
With myRng
.Sort key1:=Columns("C"), order1:=xlAscending, header:=xlNo,
MatchCase:=False, Orientation:=xlTopToBottom, key2:=Columns("B"),
order1:=xlAscending, header:=xlNo, MatchCase:=False,
Orientation:=xlTopToBottom, key3:=Columns("F"), order1:=xlAscending,
header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

any help?
thanx


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
Sorting by range planetdust New Users to Excel 3 October 21st 07 01:03 AM
Sorting a Range cmm Excel Discussion (Misc queries) 1 June 30th 06 01:35 PM
sorting range changes sum??? (excel) bertbarndoor Excel Worksheet Functions 4 March 3rd 06 04:31 PM
Sorting By a range of rows JSolesky Excel Discussion (Misc queries) 1 December 6th 05 06:26 PM
sorting a range cjsmith22 Excel Discussion (Misc queries) 2 November 13th 05 11:19 PM


All times are GMT +1. The time now is 10:02 PM.

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"