If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Range("A1").Select causes err 1004 Application-defined or object-defined error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-10, 12:27
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
Range("A1").Select causes err 1004 Application-defined or object-defined error

I am using 2007 the file is an xlsm file what the heck is wrong with 2007?

Private Sub PersistValues()
Dim lastDate As Date
Dim todaysDate As Date

Sheets("RateHistory").Activate
Range("A1").Select <-- error
Reply With Quote
  #2 (permalink)  
Old 04-23-10, 12:39
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Where / what type of module is the code sitting in?
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #3 (permalink)  
Old 04-23-10, 12:42
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
its in sheet1 i guess i should move to mod
Reply With Quote
  #4 (permalink)  
Old 04-23-10, 16:34
yoo_neek yoo_neek is offline
Registered User
 
Join Date: Apr 2010
Posts: 4
One thing I noticed with 2007 is that it does not retain the active sheet the way older versions used to...

For example:

--- code to do something in the sheet that this code is in ---
Range
Cell
etc..

--- another sheet called active from code within this sheet ---
with activesheet
.range
.cells
.whatever

Last edited by yoo_neek; 04-23-10 at 17:23.
Reply With Quote
  #5 (permalink)  
Old 04-23-10, 18:14
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi,

Quote:
Originally Posted by mikezx10 View Post
its in sheet1 i guess i should move to mod
Yes exactly. You have not qualified the parent worksheet of the range which means it will belong to the worksheet that corresponds to that sheet class module. You can only select a range on the active sheet so unless that sheet happens to be the active sheet you will get an error. Either specify the parent sheet or move the code into a standard module to correct this.

Of course, the other point to be made is that it is very uncommon to actually need to select a range with vba code... the range object can be worked with directly rather than working with the selection object. Doing it that way avoids issues such as the one you are seeing here and makes code run more quickly.
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA

Last edited by Colin Legg; 04-23-10 at 18:40.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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