Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Run-Time error '1004' : Select method of Range class failed

I have some code that works from a keyboard shortcut, but when I try
to attach it to a command button on another worksheet is delivers the
above error. Code is below, please help!

Sheets("Table").Activate
Range("E5:M17").Select
Selection.Sort Key1:=Range("M6"), Order1:=xlDescending,
Key2:=Range("L6") _
, Order2:=xlDescending, Key3:=Range("J6"),
Order3:=xlDescending, Header _
:=xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom _
, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal,
DataOption3:= _
xlSortNormal
Range("A1").Select

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Run-Time error '1004' : Select method of Range class failed

When the code is in a general module (not under a worksheet), then unqualified
ranges belong to the active sheet.

When the code is behind a worksheet, then unqualified ranges belong to the sheet
that owns the code.

So when you write:
range("e5:M17").select
it's refering to the sheet with the commandbutton--not Table

You could qualify each range:
worksheets("table).range("e5:m17").select
(kind of a pain)

Or you could drop the select's:

with worksheets("Table")
with .range("E5:M17")
.cells.sort Key1:=.columns(9), Order1:=xlDescending, _
Key2:=.columns(8), Order2:=xlDescending, _
Key3:=.columns(6), Order3:=xlDescending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
end with
end with

And you know if your data has headers, right. I'd change xlguess to xlyes or
xlno. I don't like excel to guess at anything I could specify.


wrote:

I have some code that works from a keyboard shortcut, but when I try
to attach it to a command button on another worksheet is delivers the
above error. Code is below, please help!

Sheets("Table").Activate
Range("E5:M17").Select
Selection.Sort Key1:=Range("M6"), Order1:=xlDescending,
Key2:=Range("L6") _
, Order2:=xlDescending, Key3:=Range("J6"),
Order3:=xlDescending, Header _
:=xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom _
, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal,
DataOption3:= _
xlSortNormal
Range("A1").Select


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Run-Time error '1004' : Select method of Range class failed

Thanks, works great now.

On Mar 9, 1:19 pm, Dave Peterson wrote:
One more followup...

So when you write:
range("e5:M17").select
it's refering to the sheet with the commandbutton--not Table

And since you're trying to select a range on a non-active sheet (the sheet with
the commandbutton), you get the error.





wrote:

I have some code that works from a keyboard shortcut, but when I try
to attach it to a command button on another worksheet is delivers the
above error. Code is below, please help!


Sheets("Table").Activate
Range("E5:M17").Select
Selection.Sort Key1:=Range("M6"), Order1:=xlDescending,
Key2:=Range("L6") _
, Order2:=xlDescending, Key3:=Range("J6"),
Order3:=xlDescending, Header _
:=xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom _
, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal,
DataOption3:= _
xlSortNormal
Range("A1").Select


--

Dave Peterson- Hide quoted text -

- Show quoted text -



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method 'MacroOptions' of object'_Application' failed Runtime Error 1004 EagleOne Excel Discussion (Misc queries) 1 October 22nd 14 05:49 PM
Run-time error "1004" Select method of range class failed Tallan Excel Discussion (Misc queries) 3 March 7th 07 05:22 PM
Charts.Add error '1004' Method 'Add' of object 'Sheets' failed Corey Charts and Charting in Excel 1 December 28th 06 02:15 PM
Error: method 'select' of object_worksheet' failed Carl Excel Discussion (Misc queries) 4 September 9th 06 08:52 PM
runtime error 1004 method range of object '_global failed valdesd Excel Discussion (Misc queries) 2 October 6th 05 07:26 PM


All times are GMT +1. The time now is 08:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"