sorting variable sized range

eng_khalid
06-27-2009, 11:50 AM
is there a way to sort a variable sized range?

ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=ActiveCell. _
Offset(-1, 0).Range("A1:A41"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange ActiveCell.Offset(-2, -17).Range("A1:AI42")
.Header = xlYes
.MatchCase = True
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

that's the code...is there a way to replace the " Range("A1:A41") " with a code that is flexible with the size of range?
I tried Range.CurrentRegion but it didn't work:(
any ideas
thnx in advance

dunk123
06-27-2009, 12:21 PM
Try modifying this to your needs:

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Sort Key1:=Range("f2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum