View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Problem sorting data

Where is the code? Is it behind a worksheet or in a General module?

I'd use:

with activeworkbook.Worksheets("Sheet1").Range("A1:A8")
.cells.Sort Key1:=.Range("A1"), _
Order1:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
end with

The dots mean that that thing (.cells and .range("a1")) belong to the object in
the previous with statement--in this case sheet1 of the activeworkbook.

scottydel wrote:

Hello,

I'm using Excel 2003 and am having trouble sorting data. Here is the code
that I am using:

Application.Worksheets("Sheet1").Select
Application.Worksheets("Sheet1").Range("A1:A8").Se lect
Selection.Sort Key1:=Range("A1"), _
Order1:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Here is my error message:

The sort reference is not valid. Make sure that it's within the data you
want to sort, and the first Sort By box isn't the same or blank.

The code comes alomst entirely from a the Macro I recorded, which works.
Meaning, if I select this range manually, the data sorts without a hiccup.
But if I try and execute code genned by the same sort Macro, VBA throws an
error. The only lines that did not come from the Macro are the first line,
selecting the worksheet and the "Application.Worksheets("Sheet1")." I
appended in front of Range.

Here is the data I have in Sheet1, Column A, Rows 1-8:

89100400
93092800
93701200
170074488
319529926
390162282
494995008
597663764

Totally confused.

Any help would be greatly appreciated.

Thanks,

Scott


--

Dave Peterson