Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default Why this code is not work?

I want to sort other worksheet in the same workbook,
I try to run macro from Sheet2:
-----------------------------------
Sheets("Sheet1").Select
Cells.Select
....
--------------------------------
it work find.
But when I create one button in Sheet2 and put these code under that button.
After I click (run) that button, it give me an error:

Run-time error '1004'
Select method of range class failled

Pls can someone help me?
Thanks





  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Why this code is not work?

If you qualify the ranges, it'll work:

Sheets("sheet1").select
Sheets("sheet1").cells.select

An unqualified range in a General module will refer to the activesheet.

An unqualified range behind a worksheet will refer to that worksheet that owns
the code.

And you can only select a range on the activesheet.

So your code (behind sheet2) is equivalent to:

Sheets("sheet1").select 'Sheet1 is the activesheet
Sheets("sheet2").cells.select 'and the .select fails since sheet2 is not the
activesheet.


Error code when select worksheet wrote:

I want to sort other worksheet in the same workbook,
I try to run macro from Sheet2:
-----------------------------------
Sheets("Sheet1").Select
Cells.Select
....
--------------------------------
it work find.
But when I create one button in Sheet2 and put these code under that button.
After I click (run) that button, it give me an error:

Run-time error '1004'
Select method of range class failled

Pls can someone help me?
Thanks


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2
Default Why this code is not work?

Thank you. That error id passed, and when I add the following:

Selection.Sort key1:=Range("D2"), order1:=xlAscending, key2:=Range("B2") _
, order2:=xlAscending, header:=xlYes, OrderCustom:=1,
MatchCase:=False _
, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2:= _
xlSortNormal

I got an error:
Run time error 1004
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.


But this code I run in Macro is working fine.
Can you tell where is the mistake?








"Dave Peterson" wrote:

If you qualify the ranges, it'll work:

Sheets("sheet1").select
Sheets("sheet1").cells.select

An unqualified range in a General module will refer to the activesheet.

An unqualified range behind a worksheet will refer to that worksheet that owns
the code.

And you can only select a range on the activesheet.

So your code (behind sheet2) is equivalent to:

Sheets("sheet1").select 'Sheet1 is the activesheet
Sheets("sheet2").cells.select 'and the .select fails since sheet2 is not the
activesheet.


Error code when select worksheet wrote:

I want to sort other worksheet in the same workbook,
I try to run macro from Sheet2:
-----------------------------------
Sheets("Sheet1").Select
Cells.Select
....
--------------------------------
it work find.
But when I create one button in Sheet2 and put these code under that button.
After I click (run) that button, it give me an error:

Run-time error '1004'
Select method of range class failled

Pls can someone help me?
Thanks


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Why this code is not work?

All those range()'s are unqualified.

With worksheets("Sheet1")
.select
.cells.select
selection.sort key1:=.range("d2"), ...
key2:=.range("b2"), ....

....

Those dots in front of the range()'s mean they refer to the object in the
previous With statement.

You could also do:

Selection.Sort key1:=sheets("sheet1").Range("D2"), order1:=xlAscending, _
key2:=sheets("sheet1").Range("B2") _
, order2:=xlAscending, header:=xlYes, OrderCustom:=1, _
MatchCase:=False _
, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal

Error code when select worksheet wrote:

Thank you. That error id passed, and when I add the following:

Selection.Sort key1:=Range("D2"), order1:=xlAscending, key2:=Range("B2") _
, order2:=xlAscending, header:=xlYes, OrderCustom:=1,
MatchCase:=False _
, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2:= _
xlSortNormal

I got an error:
Run time error 1004
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.

But this code I run in Macro is working fine.
Can you tell where is the mistake?

"Dave Peterson" wrote:

If you qualify the ranges, it'll work:

Sheets("sheet1").select
Sheets("sheet1").cells.select

An unqualified range in a General module will refer to the activesheet.

An unqualified range behind a worksheet will refer to that worksheet that owns
the code.

And you can only select a range on the activesheet.

So your code (behind sheet2) is equivalent to:

Sheets("sheet1").select 'Sheet1 is the activesheet
Sheets("sheet2").cells.select 'and the .select fails since sheet2 is not the
activesheet.


Error code when select worksheet wrote:

I want to sort other worksheet in the same workbook,
I try to run macro from Sheet2:
-----------------------------------
Sheets("Sheet1").Select
Cells.Select
....
--------------------------------
it work find.
But when I create one button in Sheet2 and put these code under that button.
After I click (run) that button, it give me an error:

Run-time error '1004'
Select method of range class failled

Pls can someone help me?
Thanks


--

Dave Peterson


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2
Default Why this code is not work?

Thank you very much. It work now.

"Dave Peterson" wrote:

All those range()'s are unqualified.

With worksheets("Sheet1")
.select
.cells.select
selection.sort key1:=.range("d2"), ...
key2:=.range("b2"), ....

....

Those dots in front of the range()'s mean they refer to the object in the
previous With statement.

You could also do:

Selection.Sort key1:=sheets("sheet1").Range("D2"), order1:=xlAscending, _
key2:=sheets("sheet1").Range("B2") _
, order2:=xlAscending, header:=xlYes, OrderCustom:=1, _
MatchCase:=False _
, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal

Error code when select worksheet wrote:

Thank you. That error id passed, and when I add the following:

Selection.Sort key1:=Range("D2"), order1:=xlAscending, key2:=Range("B2") _
, order2:=xlAscending, header:=xlYes, OrderCustom:=1,
MatchCase:=False _
, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2:= _
xlSortNormal

I got an error:
Run time error 1004
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.

But this code I run in Macro is working fine.
Can you tell where is the mistake?

"Dave Peterson" wrote:

If you qualify the ranges, it'll work:

Sheets("sheet1").select
Sheets("sheet1").cells.select

An unqualified range in a General module will refer to the activesheet.

An unqualified range behind a worksheet will refer to that worksheet that owns
the code.

And you can only select a range on the activesheet.

So your code (behind sheet2) is equivalent to:

Sheets("sheet1").select 'Sheet1 is the activesheet
Sheets("sheet2").cells.select 'and the .select fails since sheet2 is not the
activesheet.


Error code when select worksheet wrote:

I want to sort other worksheet in the same workbook,
I try to run macro from Sheet2:
-----------------------------------
Sheets("Sheet1").Select
Cells.Select
....
--------------------------------
it work find.
But when I create one button in Sheet2 and put these code under that button.
After I click (run) that button, it give me an error:

Run-time error '1004'
Select method of range class failled

Pls can someone help me?
Thanks

--

Dave Peterson


--

Dave Peterson



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
Number Format Code won't work tbmarlie Excel Discussion (Misc queries) 2 October 15th 07 06:46 PM
Workbook event code won't work on laptop SoupNazi Excel Worksheet Functions 1 April 20th 07 11:38 PM
searching by number or code ina work book justsomeguy Excel Discussion (Misc queries) 0 February 19th 07 08:55 PM
code is not to work on sheet1 Allan R Jeffery New Users to Excel 3 August 22nd 06 02:22 PM
Code does not work on emailed file EFWY Excel Discussion (Misc queries) 3 October 24th 05 02:52 PM


All times are GMT +1. The time now is 07:24 AM.

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"