Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
bl bl is offline
external usenet poster
 
Posts: 7
Default Application-defined or Object-defined error

Dear all,

I am puzzled in the following situtations. There are 2 worksheets in the
workbook, one for user menu and the other is the data. I would like to do
some sorting in the data. As a result, I write down the following in the
"UserMenu" worksheet.

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

However, the coding got the error message "Run-time error 1004 -
Application-defined or Object-defined error"

However, if I paste the code into the "Data" sheet as follows :

Sub Test2()
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

The code can be executed without any problem. What is the reason for the
problem and your help will be much appreciated.

BL


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Application-defined or Object-defined error

Don't place the code in Test2 in the code module behind the worksheet (which
is a class module). Insert a standard module using the VB editor and place
the code there.

When you use references such as Cells in the class module behind a sheet, it
refers to the cells in that sheet, not the active sheet.

Alternatively, you could make your references more explicit:

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(19,
10)).Select
Selection.Sort key1:=ActiveSheet.Range("F1")
End Sub

or

Sub Test2()
Sheets("Data").Select
With ActiveSheet
.Range(.Cells(1, 1), .Cells(19, 10)).Select
Selection.Sort key1:=.Range("F1")
End With
End Sub

Even better, don't select anything:

Sub Test2()
With Sheets("Data")
.Range(.Cells(1, 1), .Cells(19, 10)).Sort key1:=.Range("F1")
End With
End Sub

John Green - Excel MVP



"BL" wrote in message
...
Dear all,

I am puzzled in the following situtations. There are 2 worksheets in the
workbook, one for user menu and the other is the data. I would like to do
some sorting in the data. As a result, I write down the following in the
"UserMenu" worksheet.

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

However, the coding got the error message "Run-time error 1004 -
Application-defined or Object-defined error"

However, if I paste the code into the "Data" sheet as follows :

Sub Test2()
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

The code can be executed without any problem. What is the reason for the
problem and your help will be much appreciated.

BL




  #3   Report Post  
Posted to microsoft.public.excel.programming
bl bl is offline
external usenet poster
 
Posts: 7
Default Application-defined or Object-defined error

Hi, John,

Thank you very much for you advice. Unfortunately, my problem is not yet
solved. The user menu I use is not a class module but a standard worksheet
with some botton command only. I try to replicate the problem in other
simple worksheet. If I have only one sheet, I can write the code in the
standard module as

Sub Test()

ActiveSheet.Range(Cells(2, 2), Cells(6, 2)).Sort _
Key1:=Range("B2"), Order1:=xlAscending
End Sub

However, If I have more than one sheet, the following code is not working

Sub Test2()
Sheets("Sheet1").Range(Cells(2, 2), Cells(6, 2)).Sort _
Key1:=Range("B2"), Order1:=xlAscending
End Sub

I cannot figure out the reason. Please help.

Regards,

BL

"John Green" wrote:

Don't place the code in Test2 in the code module behind the worksheet (which
is a class module). Insert a standard module using the VB editor and place
the code there.

When you use references such as Cells in the class module behind a sheet, it
refers to the cells in that sheet, not the active sheet.

Alternatively, you could make your references more explicit:

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(19,
10)).Select
Selection.Sort key1:=ActiveSheet.Range("F1")
End Sub

or

Sub Test2()
Sheets("Data").Select
With ActiveSheet
.Range(.Cells(1, 1), .Cells(19, 10)).Select
Selection.Sort key1:=.Range("F1")
End With
End Sub

Even better, don't select anything:

Sub Test2()
With Sheets("Data")
.Range(.Cells(1, 1), .Cells(19, 10)).Sort key1:=.Range("F1")
End With
End Sub

John Green - Excel MVP



"BL" wrote in message
...
Dear all,

I am puzzled in the following situtations. There are 2 worksheets in the
workbook, one for user menu and the other is the data. I would like to do
some sorting in the data. As a result, I write down the following in the
"UserMenu" worksheet.

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

However, the coding got the error message "Run-time error 1004 -
Application-defined or Object-defined error"

However, if I paste the code into the "Data" sheet as follows :

Sub Test2()
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

The code can be executed without any problem. What is the reason for the
problem and your help will be much appreciated.

BL





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Application-defined or Object-defined error

You probably need to qualify the reference in the Key argument to
sort.

Change
Key1:=Range("B2"), Order1:=xlAscending
' to
Key1:=Sheets("Sheet1").Range("B2"), Order1:=xlAscending


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"BL" wrote in message
...
Hi, John,

Thank you very much for you advice. Unfortunately, my problem
is not yet
solved. The user menu I use is not a class module but a
standard worksheet
with some botton command only. I try to replicate the problem
in other
simple worksheet. If I have only one sheet, I can write the
code in the
standard module as

Sub Test()

ActiveSheet.Range(Cells(2, 2), Cells(6, 2)).Sort _
Key1:=Range("B2"), Order1:=xlAscending
End Sub

However, If I have more than one sheet, the following code is
not working

Sub Test2()
Sheets("Sheet1").Range(Cells(2, 2), Cells(6, 2)).Sort _
Key1:=Range("B2"), Order1:=xlAscending
End Sub

I cannot figure out the reason. Please help.

Regards,

BL

"John Green" wrote:

Don't place the code in Test2 in the code module behind the
worksheet (which
is a class module). Insert a standard module using the VB
editor and place
the code there.

When you use references such as Cells in the class module
behind a sheet, it
refers to the cells in that sheet, not the active sheet.

Alternatively, you could make your references more explicit:

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(ActiveSheet.Cells(1, 1),
ActiveSheet.Cells(19,
10)).Select
Selection.Sort key1:=ActiveSheet.Range("F1")
End Sub

or

Sub Test2()
Sheets("Data").Select
With ActiveSheet
.Range(.Cells(1, 1), .Cells(19, 10)).Select
Selection.Sort key1:=.Range("F1")
End With
End Sub

Even better, don't select anything:

Sub Test2()
With Sheets("Data")
.Range(.Cells(1, 1), .Cells(19, 10)).Sort
key1:=.Range("F1")
End With
End Sub

John Green - Excel MVP



"BL" wrote in message
...
Dear all,

I am puzzled in the following situtations. There are 2
worksheets in the
workbook, one for user menu and the other is the data. I
would like to do
some sorting in the data. As a result, I write down the
following in the
"UserMenu" worksheet.

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow,
19)).Select
Selection.Sort key1:=Range("R10")
End Sub

However, the coding got the error message "Run-time error
1004 -
Application-defined or Object-defined error"

However, if I paste the code into the "Data" sheet as
follows :

Sub Test2()
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow,
19)).Select
Selection.Sort key1:=Range("R10")
End Sub

The code can be executed without any problem. What is the
reason for the
problem and your help will be much appreciated.

BL







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Application-defined or Object-defined error

By default, workbooks do not contain any standard modules. Standard modules
can only be created by you (Insert||Module in the VB Editor window).

The modules supplied automatically by Excel that sit behind each worksheet
and the ThisWorkbook module are all CLASS modules. One characteristic of a
class module is that it can contain event procedures that respond to events
such as clicking a button.

Normally I would insert a standard module to contain procedures like your
Test procedure, which would have then worked as you expected when called
from your button's event procedure.

However, I also pointed out that you can place Test in a class module but
you must then take account of the special characteristics of a class module
where unqualified references such as Cells(1,2) refer to the cells in the
sheet associated with the class module, not the active sheet and
Range("R10") refers to R10 in the associated sheet, not the active sheet.
Therefore, you need to make your references more explicit. For example,
ActiveSheet.Cells(1,2) and Sheet("Data").Range("R10") as I did in the
examples I gave you.

John Green - Excel MVP

"BL" wrote in message
...
Hi, John,

Thank you very much for you advice. Unfortunately, my problem is not yet
solved. The user menu I use is not a class module but a standard worksheet
with some botton command only. I try to replicate the problem in other
simple worksheet. If I have only one sheet, I can write the code in the
standard module as

Sub Test()

ActiveSheet.Range(Cells(2, 2), Cells(6, 2)).Sort _
Key1:=Range("B2"), Order1:=xlAscending
End Sub

However, If I have more than one sheet, the following code is not working

Sub Test2()
Sheets("Sheet1").Range(Cells(2, 2), Cells(6, 2)).Sort _
Key1:=Range("B2"), Order1:=xlAscending
End Sub

I cannot figure out the reason. Please help.

Regards,

BL

"John Green" wrote:

Don't place the code in Test2 in the code module behind the worksheet
(which
is a class module). Insert a standard module using the VB editor and
place
the code there.

When you use references such as Cells in the class module behind a sheet,
it
refers to the cells in that sheet, not the active sheet.

Alternatively, you could make your references more explicit:

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(19,
10)).Select
Selection.Sort key1:=ActiveSheet.Range("F1")
End Sub

or

Sub Test2()
Sheets("Data").Select
With ActiveSheet
.Range(.Cells(1, 1), .Cells(19, 10)).Select
Selection.Sort key1:=.Range("F1")
End With
End Sub

Even better, don't select anything:

Sub Test2()
With Sheets("Data")
.Range(.Cells(1, 1), .Cells(19, 10)).Sort key1:=.Range("F1")
End With
End Sub

John Green - Excel MVP



"BL" wrote in message
...
Dear all,

I am puzzled in the following situtations. There are 2 worksheets in
the
workbook, one for user menu and the other is the data. I would like to
do
some sorting in the data. As a result, I write down the following in
the
"UserMenu" worksheet.

Sub Test2()
Sheets("Data").Select
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

However, the coding got the error message "Run-time error 1004 -
Application-defined or Object-defined error"

However, if I paste the code into the "Data" sheet as follows :

Sub Test2()
ActiveSheet.Range(Cells(Firstrow, 1), Cells(Lastrow, 19)).Select
Selection.Sort key1:=Range("R10")
End Sub

The code can be executed without any problem. What is the reason for
the
problem and your help will be much appreciated.

BL







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
Run-time error '50290': Application-defined or object-defined erro Macro button Excel Discussion (Misc queries) 1 March 12th 09 10:59 AM
Application-Defined or Object-Defined Error on simple code [email protected] Excel Programming 6 February 22nd 05 09:03 PM
Application-defined or object-defined error on copy Josh Sale Excel Programming 1 February 3rd 05 01:40 AM
Application-defined or object-defined error - missing the basics [email protected] Excel Programming 1 December 28th 04 10:23 PM
Macro Run-time Error 1004 Application Defined or Object Defined Error Anddmx Excel Programming 6 June 9th 04 03:40 PM


All times are GMT +1. The time now is 06:56 AM.

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

About Us

"It's about Microsoft Excel"