#1   Report Post  
ch
 
Posts: n/a
Default Selection Range

My VBA sometimes works fine with
range("A1").select

Bt sometimes, it shows an error unless I include a "activesheet" in front as
below :
activesheet.range("A1").select

Anyone please help me know why the additional "word" is needed ?
  #2   Report Post  
Bob Phillips
 
Posts: n/a
Default

Could it be that it is a chart sheet that is active, that is a sheet that
doesn't have a range.

--
HTH

Bob Phillips

"ch" wrote in message
...
My VBA sometimes works fine with
range("A1").select

Bt sometimes, it shows an error unless I include a "activesheet" in front

as
below :
activesheet.range("A1").select

Anyone please help me know why the additional "word" is needed ?



  #3   Report Post  
ch
 
Posts: n/a
Default

The original text did not turn out correctly. I have removed the inverted
commas in the following lines.

My VBA sometimes works fine with
range(A1).select

Bt sometimes, it shows an error unless I include a activesheet in front as
below :
activesheet.range(A1).select

Anyone please help me know why the additional "word" is needed ?

"ch" wrote:

My VBA sometimes works fine with
range("A1").select

Bt sometimes, it shows an error unless I include a "activesheet" in front as
below :
activesheet.range("A1").select

Anyone please help me know why the additional "word" is needed ?

  #4   Report Post  
ch
 
Posts: n/a
Default

I don't think so. I do not work on chart sheets at all in VBA and normal
Excel.

"Bob Phillips" wrote:

Could it be that it is a chart sheet that is active, that is a sheet that
doesn't have a range.

--
HTH

Bob Phillips

"ch" wrote in message
...
My VBA sometimes works fine with
range("A1").select

Bt sometimes, it shows an error unless I include a "activesheet" in front

as
below :
activesheet.range("A1").select

Anyone please help me know why the additional "word" is needed ?




  #5   Report Post  
Dave Peterson
 
Posts: n/a
Default

My bet is that
range("a1").select
works fine in a General module, but you're having trouble when the code is in a
worksheet module.

When you type:
range("a1").select
Then this unqualified range will refer to the activesheet in a General module,
but it'll refer to the sheet that owns the code when it's in the worksheet
module.

And I'm betting you do something like this (behind sheet1):

worksheets("Sheet2").select
range("a1").select

Since that second line is still refering to sheet1 and since you can't select a
range on a sheet that is not active, then you get the error.

You could do:
with worksheets("sheet2")
.select
.range("a1").select
end with

But if you're changing a value (say), you could just work on it directly:

worksheets("sheet2").range("a1").value = "hi There!"

Then you don't need to select the sheet or the range.

ch wrote:

My VBA sometimes works fine with
range("A1").select

Bt sometimes, it shows an error unless I include a "activesheet" in front as
below :
activesheet.range("A1").select

Anyone please help me know why the additional "word" is needed ?


--

Dave Peterson


  #6   Report Post  
ch
 
Posts: n/a
Default

Thanks Dave! You found the source of my problem!

"Dave Peterson" wrote:

My bet is that
range("a1").select
works fine in a General module, but you're having trouble when the code is in a
worksheet module.

When you type:
range("a1").select
Then this unqualified range will refer to the activesheet in a General module,
but it'll refer to the sheet that owns the code when it's in the worksheet
module.

And I'm betting you do something like this (behind sheet1):

worksheets("Sheet2").select
range("a1").select

Since that second line is still refering to sheet1 and since you can't select a
range on a sheet that is not active, then you get the error.

You could do:
with worksheets("sheet2")
.select
.range("a1").select
end with

But if you're changing a value (say), you could just work on it directly:

worksheets("sheet2").range("a1").value = "hi There!"

Then you don't need to select the sheet or the range.

ch wrote:

My VBA sometimes works fine with
range("A1").select

Bt sometimes, it shows an error unless I include a "activesheet" in front as
below :
activesheet.range("A1").select

Anyone please help me know why the additional "word" is needed ?


--

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
Cannot Expand Named Range - when size of the Range exceeds Snig Excel Discussion (Misc queries) 1 July 7th 05 01:46 PM
can a formula check for a certain value in a range? Lee IT Excel Discussion (Misc queries) 1 April 7th 05 04:31 PM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM
random selection from a range of cells tjb Excel Worksheet Functions 1 February 15th 05 06:34 PM
named range refers to: in a chart Spencer Hutton Excel Discussion (Misc queries) 1 December 14th 04 10:15 PM


All times are GMT +1. The time now is 01:54 PM.

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"