Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Programatically sort worksheet column

The challenge I am dealing with is sorting a single column in a worksheet.

I recorded a sort macro using Excel 2007 and it works fine in the program.
When the file is saved in compatibility mode the sort function does not work
when run on a computer with Excel 2002. If I record a sort macro using Excel
2002, the program works fine when using Excel 2002 but will not work when the
program is opened with Excel 2007.

Can anyone help me write code for sorting a single column in a worksheet
that will work in all versions of Excel from 97 to 2007? Any assistance would
be greatly appreciated.

Paul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 527
Default Programatically sort worksheet column

Something like this

Sub SortInsForOut()
' macro to sort deliveries! on 2 two keys
Dim rng
Application.ScreenUpdating = False

Range("A1").Select
rng = Range("a1").CurrentRegion.Address
Range(rng).Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range( _
"D2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
MatchCase _
:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal

Sheets("DispenseForm").Activate
Application.ScreenUpdating = True
End Sub

The trick is to get the address on each sheet and use this in the sort
macros.
these are the key lines

Range("A1").Select
rng = Range("a1").CurrentRegion.Address
Range(rng).Sort Key1:=Range("B2")

copy these into your macro changing the range to suit.

Peter
"Paul D." wrote:

The challenge I am dealing with is sorting a single column in a worksheet.

I recorded a sort macro using Excel 2007 and it works fine in the program.
When the file is saved in compatibility mode the sort function does not work
when run on a computer with Excel 2002. If I record a sort macro using Excel
2002, the program works fine when using Excel 2002 but will not work when the
program is opened with Excel 2007.

Can anyone help me write code for sorting a single column in a worksheet
that will work in all versions of Excel from 97 to 2007? Any assistance would
be greatly appreciated.

Paul

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Programatically sort worksheet column

Perhaps I was a bit vague, but I was trying to get this to work in VB as a
response to a button click as opposed to a macro for a worksheet. Although
what you sent may be helpful I am having difficulty applying it.

The following works in Excel 2007 but not in Excel 2003. Can you tell from
what I have below why it might not be working in the earlier version.

Private Sub CommandButton1_Click()
'sngCountAns is the number of entries contained in the target column
strAddApt = Me.TextBox1.Value
If Len(strAddApt) = 4 Then
Sheets("Lists").Range("P" & sngCountAns + 1).Value = strAddApt
ActiveWorkbook.Worksheets("Lists").Sort.SortFields .Clear
ActiveWorkbook.Worksheets("Lists").Sort.SortFields .Add Key:=Range("P2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Lists").Sort
.SetRange Range("P2:P" & sngCountAns + 1)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
UserForm_Activate
Me.TextBox1.Value = ""
Else
MsgBox ("Airport ID must be 4 digits to be added to the list")
End If
End Sub

"Billy Liddel" wrote:

Something like this

Sub SortInsForOut()
' macro to sort deliveries! on 2 two keys
Dim rng
Application.ScreenUpdating = False

Range("A1").Select
rng = Range("a1").CurrentRegion.Address
Range(rng).Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range( _
"D2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
MatchCase _
:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal

Sheets("DispenseForm").Activate
Application.ScreenUpdating = True
End Sub

The trick is to get the address on each sheet and use this in the sort
macros.
these are the key lines

Range("A1").Select
rng = Range("a1").CurrentRegion.Address
Range(rng).Sort Key1:=Range("B2")

copy these into your macro changing the range to suit.

Peter
"Paul D." wrote:

The challenge I am dealing with is sorting a single column in a worksheet.

I recorded a sort macro using Excel 2007 and it works fine in the program.
When the file is saved in compatibility mode the sort function does not work
when run on a computer with Excel 2002. If I record a sort macro using Excel
2002, the program works fine when using Excel 2002 but will not work when the
program is opened with Excel 2007.

Can anyone help me write code for sorting a single column in a worksheet
that will work in all versions of Excel from 97 to 2007? Any assistance would
be greatly appreciated.

Paul

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
Programatically sort and highlight Rookie_User Excel Discussion (Misc queries) 0 January 5th 10 05:52 PM
How do I sort column changes in different worksheet? chewy70 Excel Worksheet Functions 3 March 24th 09 02:26 AM
Sort Scenarios in List manually or programatically mcambrose Excel Programming 0 October 17th 06 07:49 PM
Can I sort a column of #s Rt to left? in a worksheet Concha94 Excel Discussion (Misc queries) 1 June 1st 06 07:16 PM
Sort by column inside a worksheet flyingrhino Excel Worksheet Functions 2 April 10th 06 09:42 PM


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

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

About Us

"It's about Microsoft Excel"