syntax for a data sort

OutlawoftheMarsh

New Member
Joined
Dec 11, 2003
Messages
35
I'm on Sheet1. I need to sort the data on Sheet2 without leaving Sheet1. When Sheet2 is the active sheet, my macro runs fine. When Sheet 1 is the active sheet, I get a 1004 error. I have no idea what that means. Here's my code:

Sheets("Sheet2").Columns("A:C").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

I'm not sure which part is wrong, but I think it might have something to do with the Range("A1") part.

Thanks,

Outlaw
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
In my testing, I wasn't able to sort a range not on the active sheet. There might very well be a way, I just didn't find it.

Just out of curiousity, why can't you go to Sheet2, sort it, and then go back?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
Is that all of your code?

I think you might want to make a reference to the sheet you want to sort:

Sheets("Sheet2").Columns("A:C").Sort Key1:=Sheets("Sheet2").Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 

OutlawoftheMarsh

New Member
Joined
Dec 11, 2003
Messages
35
Thanks, Norie. Your suggestion fixed it perfectly. I thought I had the syntax wrong, but my help files are missing, so it's tough.

In response to Tazguy's question, I am creating a workbook to help with learning new vocabulary (among other things). It's kind of like electronic flashcards. As such, I'm going to be sorting the data again and again in relatively rapid succession, so it would give me a headache watching the screen switch back and forth so much. I guess I could put the vocab list way off to the right somewhere on one worksheet and put the "flashcard" on there on the left, but I prefer having different pages (and learning more about VBA).

Thanks again for the help!

Outlaw of the Marsh
 

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
With Application.ScreenUpdating set to false, you wouldn't see the windows going back and forth.
 

Forum statistics

Threads
1,148,057
Messages
5,744,550
Members
423,882
Latest member
Seeham

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