#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Sort Method

I have the following code. For some reason Key2 is not being sorted the way
I need it. For my santity, I tried switching between ascending/descending
in key2 and both results same. Any ideas why? The sort range is columns
A:AJ. Column B contains numbers and Column D contains dates.


Private Sub Workbook_BeforeClose(Cancel As Boolean)

'sort contract workbook (Sheet1) only if Settlement4b.xls is not open

Dim Wsheet As Worksheet
Set Wsheet = Worksheets("Sheet1") 'Contracts workbook

If Not WorkbookOpen("Settlement4b.xls") Then
With Wsheet
.Select
.Range("A1").Sort _
Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlYes, _
Key2:=.Range("D1"), Order1:=xlDescending, Header:=xlYes
End With
End If


As aways Thanks!
Mike

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 320
Default Sort Method

A few things. You don't want to sort range A1, you probably want
Range("A1").CurrentRegion, or maybe Range("A1:AJ500")
Next - you don't want to say Header:=xlyes twice, tho it doesn't seem to
hurt.
"Squid" wrote in message
news:Po01c.457337$na.1094918@attbi_s04...
I have the following code. For some reason Key2 is not being sorted the

way
I need it. For my santity, I tried switching between ascending/descending
in key2 and both results same. Any ideas why? The sort range is columns
A:AJ. Column B contains numbers and Column D contains dates.


Private Sub Workbook_BeforeClose(Cancel As Boolean)

'sort contract workbook (Sheet1) only if Settlement4b.xls is not open

Dim Wsheet As Worksheet
Set Wsheet = Worksheets("Sheet1") 'Contracts workbook

If Not WorkbookOpen("Settlement4b.xls") Then
With Wsheet
.Select
.Range("A1").Sort _
Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlYes, _
Key2:=.Range("D1"), Order1:=xlDescending, Header:=xlYes
End With
End If


As aways Thanks!
Mike

End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sort Method

You shouldn't have Header in twice - header is for the entire sort area, not
for each key.

Other than that, I can't say. Try turning on the macro recorder and
sorting it manually. See what is recorded.

--
Regards,
Tom Ogilvy


"Squid" wrote in message
news:Po01c.457337$na.1094918@attbi_s04...
I have the following code. For some reason Key2 is not being sorted the

way
I need it. For my santity, I tried switching between ascending/descending
in key2 and both results same. Any ideas why? The sort range is columns
A:AJ. Column B contains numbers and Column D contains dates.


Private Sub Workbook_BeforeClose(Cancel As Boolean)

'sort contract workbook (Sheet1) only if Settlement4b.xls is not open

Dim Wsheet As Worksheet
Set Wsheet = Worksheets("Sheet1") 'Contracts workbook

If Not WorkbookOpen("Settlement4b.xls") Then
With Wsheet
.Select
.Range("A1").Sort _
Key1:=.Range("B1"), Order1:=xlAscending, Header:=xlYes, _
Key2:=.Range("D1"), Order1:=xlDescending, Header:=xlYes
End With
End If


As aways Thanks!
Mike

End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Sort Method

Your code is sorting only range A1. You need to sort all the
columns. Change

..Range("A1").Sort _
to
..UsedRange.Sort (...)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Squid" wrote in message
news:Po01c.457337$na.1094918@attbi_s04...
I have the following code. For some reason Key2 is not being

sorted the way
I need it. For my santity, I tried switching between

ascending/descending
in key2 and both results same. Any ideas why? The sort range

is columns
A:AJ. Column B contains numbers and Column D contains dates.


Private Sub Workbook_BeforeClose(Cancel As Boolean)

'sort contract workbook (Sheet1) only if Settlement4b.xls is

not open

Dim Wsheet As Worksheet
Set Wsheet = Worksheets("Sheet1") 'Contracts workbook

If Not WorkbookOpen("Settlement4b.xls") Then
With Wsheet
.Select
.Range("A1").Sort _
Key1:=.Range("B1"), Order1:=xlAscending,

Header:=xlYes, _
Key2:=.Range("D1"), Order1:=xlDescending,

Header:=xlYes
End With
End If


As aways Thanks!
Mike

End Sub




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
VBA Sort method w/more than 3 Keys KenRoy Excel Discussion (Misc queries) 1 August 26th 05 10:48 PM
Sort Method Questions pikus Excel Programming 2 February 3rd 04 03:46 PM
To sort data in VBA/excel by the method of the minimas Jean-mi Excel Programming 1 December 11th 03 09:26 AM
sort method frustration David Wenham Excel Programming 2 August 21st 03 06:26 PM
Sort method of range Richard Clarke Excel Programming 7 July 25th 03 04:42 PM


All times are GMT +1. The time now is 05:14 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"