Sort works as independent sub() but not within a larger sub()

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
128
Office Version
  1. 2019
Platform
  1. Windows
Today I found a sort sub routine on in this forum that allows me to sort columns left to right. To test it out, I created a simple sub routine (see below) and it worked just fine.
Sub Test()
Worksheets("CommonData").Sort.SortFields.Clear
Worksheets("CommonData").Range("E3:X24").Sort Key1:=Range("E3:X3"), Order1:=xlAscending, Orientation:=xlLeftToRight
End Sub

I copied and inserted the code into a large subroutine and upon running it, I was confronted with an error code. Below is an excerpt of that code which includes the above-mentioned code along with what immediately precedes it and the error code I got.

' Note: The following sort first sorts a table from Top to Bottom in Ascending order.
Dim rng1 As Range
Dim ws1 As Worksheet

Set ws1 = Worksheets("CommonData")
Set rng1 = ws1.Range("B5:X24")

ws1.Sort.SortFields.Clear

With rng1
.Sort Key1:=ws1.Range("B5"), Order1:=xlAscending, _
Header:=xlNo
End With

''' TESTING OUT LEFT TO RIGHT SORT OF COMMONDATA TABLE
' Note: The following sort is intended to take the same table sorted above but then also sorts it from Left To Right.
Worksheets("CommonData").Sort.SortFields.Clear
Worksheets("CommonData").Range("E3:X24").Sort Key1:=Range("E3:X3"), Order1:=xlAscending, Orientation:=xlLeftToRight

1673322692247.png

Upon receiving this message, I click on Debug and the following line is highlighted:
Worksheets("CommonData").Range("E3:X24").Sort Key1:=Range("E3:X3"), Order1:=xlAscending, Orientation:=xlLeftToRight

If anyone has any thoughts as to why this is happening or how to correct it, I would greatly appreciate your help.

Thank you
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Upon receiving this message, I click on Debug and the following line is highlighted:
Worksheets("CommonData").Range("E3:X24").Sort Key1:=Range("E3:X3"), Order1:=xlAscending, Orientation:=xlLeftToRight
My guess is that when you ran the code that produced that error, 'CommonData' was not the active sheet.
That means the the blue code is trying to sort data on 'CommonData' but the red code is a range on the active sheet, not 'CommonData' and therefore not within the sort data as stated by the error message.

You would have received the same error message with that small test code if you had run that when 'CommonData' was not active too.

Try changing that line to
Rich (BB code):
Worksheets("CommonData").Range("E3:X24").Sort Key1:=Worksheets("CommonData").Range("E3:X3"), Order1:=xlAscending, Orientation:=xlLeftToRight
 
Upvote 0
Solution
Thank you very much... that simple modification worked. And I appreciate your explanation as to why it did not work.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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