Data won't sort

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got a macro that opens a text file, formats it, adds headers, and the
sorts the data. Everything works EXCEPT for the sorting. I get this error.

"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."

Here is where it hangs up. This was more or less recorded and used as is.


Worksheets(MyWorkSheet).Range("B2").CurrentRegion.Select

Selection.Sort Key1:=Range("A2"), Order1:=xlAscending,
Key2:=Range("D2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal

TIA,

Paul Hammond
Richmond, Virginia
 
Why do you say B2 current region select and then try to use column A as a
key?

If column A is empty, then you will get this error.

If you are running this code from a commandbutton, then Range("A1") refers
to the sheet containing the code, not the sheet you are trying to sort. If
that is the case then (and range("A1") is not empty)

With Worksheets(MyWorkSheet).Range("B2")
.CurrentRegion.Sort Key1:=.Range("A2"), Order1:=xlAscending, _
Key2:=.Range("D2"), Order2:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNorma
End With

l
 
Actually I had something exactly like that, for some reason it did not work.
Your's did, must have been a comma somewhere.

Thanks,

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top