named range sort

  • Thread starter Thread starter Legacy 15162
  • Start date Start date
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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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.
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,222,195
Messages
6,164,513
Members
451,900
Latest member
lamski

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
Back
Top