Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Time Error '1004' and Range Class

Status
Not open for further replies.

dawa1234

Programmer
Oct 2, 2002
6
US
I keep getting this error that says "Run Time Error '1004' Select Method of Range Class Failed" and if I debug it, it points to ".Range(sortRange).Select"

Code:
Public Sub Sorts()
  With Worksheets(sheetName)
     .Range(sortRange).Select
      Selection.Sort Key1:=Range(Key),
         Order1:=xlAscending, Header:=xlGuess, _
         OrderCustom:=1, MatchCase:=False,  
         Orientation:=xlTopToBottom
      Range("A7").Select
  End With
End Sub

and if I take the "." out before the statement ".Range(sortRange).Select" I get the error "Run Time Error '1004' Sort Method of Range Class Failed." If I debug this, the VBE points to "Selection.Sort..."
There are two odd behaviors with this code. First of all, I only get this error from one particular function call, and second of all, if I am stepping through the program, the error does not show up and the program executes sucessfully. Does anyone have any ideas?


[neutral]
dawa1234
 
>> and if I take the "." out before the statement ".Range
>>(sortRange).Select" I get the error "Run Time
>>Error '1004' Sort Method of Range Class Failed."
>> If I debug this, the VBE points to "Selection.Sort..."

If you take out the ".", Excel assumes the activesheet and I'm assuming that SortRange is not defined on the activesheet.

Though this might not help with the problem, why not change:
.Range(sortRange).Select
Selection.Sort Key1:=Range(Key),
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Range("A7").Select

to:
.Range(sortRange).Sort Key1:=Range(Key),
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom

----------------------------
There are two odd behaviors with this code. First of all, I only get this error from one particular function call, and second of all, if I am stepping through the program, the error does not show up and the program executes sucessfully. Does anyone have any ideas?
----------------------------

Maybe...I've run into something similar. Do you have command buttons on your spreadsheet? If so, change the TakeFocusOnClick property to false.

HTH

 
aMember,
I found that as long as I didn't put the call to the sort routine in the activate procedure of a worksheet, it seemed to work fine. I think I have found a valid work around though. I do have command buttons; however, why do you say to change the TakeFocusOn click Property to false? I am not sure as though I understand what side effects this property really has.

Thanks.
dawa1234 [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top