Sort problem using Range.Sort from a different sheet than the one being sorted

ss123

New Member
Joined
Mar 4, 2014
Messages
23
Excel 2010:

I am using the Range.Sort method. When run, sometimes I get the following error:
Runtime error 1004: The sort reference is not valid. Make sure it is within the data you want to sort and the first sort by box isn't the same or blank.

Right now I am working with a static table so nothing is changing in it. Yet, VBA pops up with the error more often than not, especially if I run the macro from a button on a different sheet than the one being sorted.

The command it is having issues with is: Sheets("Data").Range("A1:B61").Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlYes

The "Data" sheet is very simple. It has a column header called 'Name' (A1) and one called 'Score' (B1). Though the values under 'Name' do repeat often, that should not affect a Sort since I am sorting on 'Score' or B1. In the future, when my Data sheet is filled with thousands upon thousands of records, the Score values may repeat but a normal physical Sort done within Excel (not VBA) does not have an issue with duplicate values at all. So this error does not make sense.

The only thing that might be causing issues is when I am running the command from another sheet through a button. But since I explicitly tell it run the sort on the "Data" sheet it should not be having an issue. Frustrating!

Any suggestions would be appreciated.

Thx
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
How about
VBA Code:
Sheets("Data").Range("A1:B61").Sort Key1:=Sheets("Data").Range("B1"), Order1:=xlDescending, Header:=xlYes
 
Upvote 0
Solution
Many thanks, it worked. I had thought once you tell VBA's sort command which sheet you want to sort to, it would understand to apply the key to that sheet. My fault!
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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