Sort Question

P

Patrick Simonds

I want to use the following code to sort a worksheet (Employee_List), but I
do not want the sorted worksheet to be active:

Range("A3:D300").Select

Selection.Sort Key1:=Range("D4"), Order1:=xlAscending, Key2:=Range("A4")
_
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=2, MatchCase:=
_
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
Range("A3").Select
 
G

Gary Keramidas

this doesn't have to have the sheet active

Option Explicit
Sub sort()
Dim wks As Worksheet
Set wks = Worksheets("sheet1")
With wks.Range("A3:D300")
..sort Key1:=wks.Range("D4"), Order1:=xlAscending, Key2:=wks.Range("A4"), _
Order2:=xlAscending, Header:=xlGuess, OrderCustom:=2, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
Range("A3").Select
End With
End Sub
 
P

Patrick Simonds

OK this is exactly what I put in, but it did not do anything:

Option Explicit
Sub sort()
Dim wks As Worksheet
Set wks = Worksheets("Employee_List")
With wks.Range("A3:D300")
..sort Key1:=wks.Range("D4"), Order1:=xlAscending, Key2:=wks.Range("A4"), _
Order2:=xlAscending, Header:=xlGuess, OrderCustom:=2, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
Range("A3").Select
End With

End Sub
 
P

Patrick Simonds

Got it to work, At the end of the .sort line it should have been B4 not A4.
Thanks for your help.
 
G

Gary Keramidas

if sheet 2 is the active sheet and run this, it sorts the employee_list
sheet test data i have. are you sure the sort keys are correct?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top