ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Selection Range (https://www.excelbanter.com/excel-discussion-misc-queries/35025-selection-range.html)

ch

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 ?

Bob Phillips

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 ?




ch

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 ?


ch

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 ?





Dave Peterson

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

ch

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



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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com