named range sort

L

Legacy 15162

Guest
I am utilizing access to dump data in an excel spreadsheet. after the data is dumped, i would like to sort the data by a specific column in excel. I already have a named range called sort that was defined in excel. how can i get this code to work properly?

If xlWS.Name = "Outerlier Report" Then
xlWS.Range("B5") = TxtBeginPeriod & " THRU " & TxtEndPeriod
xlWS.Cells.Name("SORT").Activate
xlWS.Cells.Sort Key1:=Range("V12"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End If
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Maybe:

Code:
With xlWS.Range("SORT")
   .Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With

You may need to change .Cells(1,1) if you don't want to sort by the first column.
 
L

Legacy 15162

Guest
If xlWS.Name = "Outerlier Report" Then
xlWS.Range("B5") = TxtBeginPeriod & " THRU " & TxtEndPeriod
With xlWS.Range("SORT")
.Sort Key1:=Range("V12"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End If


I guess the WITH statement is the key........When would you normally utilize the with statement?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
I only used With because I wanted to reference that range in the SortKey1 argument. If you know V12 is within the range then this will do it:

Code:
xlWS.Range("SORT").Sort Key1:=Range("V12"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,115
Messages
5,768,200
Members
425,459
Latest member
Danniey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top