View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Billy Liddel Billy Liddel is offline
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