ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Set Range.....am I to blind? (https://www.excelbanter.com/excel-programming/401889-set-range-am-i-blind.html)

carlo

Set Range.....am I to blind?
 
Hi All

I tried following simple code:
'-----------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20))
'-----------------------------------------------------------

It only seems to work, when I activate Sheet2! I can't remember this
ever happened. I am not trying to select anything, so why does it have
to be active?
Do I become senile? This thing is driving me crazy!

thanks for any help

Carlo

Ross Culver[_2_]

Set Range.....am I to blind?
 
Which module are you working in?

Try using Sheets("Sheet2). instead of worksheets(...

Ross


"carlo" wrote in message
...
Hi All

I tried following simple code:
'-----------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20))
'-----------------------------------------------------------

It only seems to work, when I activate Sheet2! I can't remember this
ever happened. I am not trying to select anything, so why does it have
to be active?
Do I become senile? This thing is driving me crazy!

thanks for any help

Carlo




carlo

Set Range.....am I to blind?
 
Hi Ross, thank you for the fast answer!

I am in a module called "copy".
I first put the code into the worksheet directly, but
because I will copy from different worksheets i put
it in a Module.

I also tried Sheets already but the same problem occurs:
"Application-defined or Object-defined Error"

It should work, shouldn't it?

Thanks again

Carlo

On Nov 29, 11:00 am, "Ross Culver" wrote:
Which module are you working in?

Try using Sheets("Sheet2). instead of worksheets(...

Ross

"carlo" wrote in message

...



Hi All


I tried following simple code:
'-----------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20))
'-----------------------------------------------------------


It only seems to work, when I activate Sheet2! I can't remember this
ever happened. I am not trying to select anything, so why does it have
to be active?
Do I become senile? This thing is driving me crazy!


thanks for any help


Carlo- Hide quoted text -


- Show quoted text -



Jim Cone

Set Range.....am I to blind?
 
Carlo,
"cells" is not qualified and therefore refers to the active sheet.
Note the dots in the following...
With worksheets("sheet2")
set copy_rng = .range(.cells(1,1),.cells(1,20))
End With
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)


"carlo"
wrote in message
Hi All
I tried following simple code:
'-----------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20))
'-----------------------------------------------------------
It only seems to work, when I activate Sheet2! I can't remember this
ever happened. I am not trying to select anything, so why does it have
to be active?
Do I become senile? This thing is driving me crazy!
thanks for any help
Carlo

Basilisk96

Set Range.....am I to blind?
 
On Nov 28, 8:52 pm, carlo wrote:
Do I become senile? This thing is driving me crazy!


I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)

Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.

;)
Cheers,
-Basilisk96

Ross Culver[_2_]

Set Range.....am I to blind?
 
Try dropping the "set".



"carlo" wrote in message
...
Hi Ross, thank you for the fast answer!

I am in a module called "copy".
I first put the code into the worksheet directly, but
because I will copy from different worksheets i put
it in a Module.

I also tried Sheets already but the same problem occurs:
"Application-defined or Object-defined Error"

It should work, shouldn't it?

Thanks again

Carlo

On Nov 29, 11:00 am, "Ross Culver" wrote:
Which module are you working in?

Try using Sheets("Sheet2). instead of worksheets(...

Ross

"carlo" wrote in message

...



Hi All


I tried following simple code:
'-----------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20))
'-----------------------------------------------------------


It only seems to work, when I activate Sheet2! I can't remember this
ever happened. I am not trying to select anything, so why does it have
to be active?
Do I become senile? This thing is driving me crazy!


thanks for any help


Carlo- Hide quoted text -


- Show quoted text -





carlo

Set Range.....am I to blind?
 
Thanks Basilisk96

I just tried a little bit more and i found out following:
'-------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range("A1:T1") '<--works
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20)) '<--
works not
set copy_rng =
worksheets("sheet2").range(worksheets("sheet2").ce lls(1,1),worksheets("sheet2").cells(1,20))
'<-- works
'-------------------------------------------------------

i have to give Excel the parent to all the cells!
I thought the cells() in the range object are just for the addresses
sake, but I was wrong, they have to be on the same sheet, so I need to
specify the parentsheet for every cell.

thank you very much, sorry for the confusion.

cheers and thanks

Carlo




On Nov 29, 11:06 am, Basilisk96 wrote:
On Nov 28, 8:52 pm, carlo wrote:

Do I become senile? This thing is driving me crazy!


I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)

Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.

;)
Cheers,
-Basilisk96



carlo

Set Range.....am I to blind?
 
Thank you all for replying.

@Ross:
the Set is mandatory because copy_rng is an object!

Carlo


On Nov 29, 11:12 am, carlo wrote:
Thanks Basilisk96

I just tried a little bit more and i found out following:
'-------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range("A1:T1") '<--works
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20)) '<--
works not
set copy_rng =
worksheets("sheet2").range(worksheets("sheet2").ce lls(1,1),worksheets("shee-t2").cells(1,20))
'<-- works
'-------------------------------------------------------

i have to give Excel the parent to all the cells!
I thought the cells() in the range object are just for the addresses
sake, but I was wrong, they have to be on the same sheet, so I need to
specify the parentsheet for every cell.

thank you very much, sorry for the confusion.

cheers and thanks

Carlo

On Nov 29, 11:06 am, Basilisk96 wrote:



On Nov 28, 8:52 pm, carlo wrote:


Do I become senile? This thing is driving me crazy!


I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)


Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.


;)
Cheers,
-Basilisk96- Hide quoted text -


- Show quoted text -



Ross Culver[_2_]

Set Range.....am I to blind?
 
Yeah. I never use objects, only cell/range references and variables. In
fact, if you would and when you have time, briefly explain the why bother to
use an object at all? I need to learn something new today.

Ross

"carlo" wrote in message
...
Thank you all for replying.

@Ross:
the Set is mandatory because copy_rng is an object!

Carlo


On Nov 29, 11:12 am, carlo wrote:
Thanks Basilisk96

I just tried a little bit more and i found out following:
'-------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range("A1:T1") '<--works
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20)) '<--
works not
set copy_rng =
worksheets("sheet2").range(worksheets("sheet2").ce lls(1,1),worksheets("shee-t2").cells(1,20))
'<-- works
'-------------------------------------------------------

i have to give Excel the parent to all the cells!
I thought the cells() in the range object are just for the addresses
sake, but I was wrong, they have to be on the same sheet, so I need to
specify the parentsheet for every cell.

thank you very much, sorry for the confusion.

cheers and thanks

Carlo

On Nov 29, 11:06 am, Basilisk96 wrote:



On Nov 28, 8:52 pm, carlo wrote:


Do I become senile? This thing is driving me crazy!


I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)


Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.


;)
Cheers,
-Basilisk96- Hide quoted text -


- Show quoted text -





Chip Pearson

Set Range.....am I to blind?
 
The problem is that

worksheets("sheet2").range

refers to Sheet2 but

cells(1,1),cells(1,20)

refers to the active sheet, and if Sheet2 is not the active sheet, you will
be attempting to set a range on Sheet2 to cells on the active sheet. Fully
qualified, your code is interpreted as:

set copy_rng =
Worksheets("Sheet2").Range(ActiveSheet.Cells(1,1), ActiveSheet(1,20))

You should write your code as

With Worksheets("Sheet2")
Set copy_rng = .Range(.Cells(1,1),.Cells(1,20))
End With

Note that there is a leading period before "Range", and the two "Cells".
This is required for the With statement.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"carlo" wrote in message
...
Hi All

I tried following simple code:
'-----------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20))
'-----------------------------------------------------------

It only seems to work, when I activate Sheet2! I can't remember this
ever happened. I am not trying to select anything, so why does it have
to be active?
Do I become senile? This thing is driving me crazy!

thanks for any help

Carlo



Chip Pearson

Set Range.....am I to blind?
 

the why bother to
use an object at all?


Because the entire object model -- the application's very definition -- is
comprised almost completely of objects, so using the built-in object types
allows your code to mesh seamlessly with the native Excel application. To
understand objects, you need to understand classes; an object is a class
that is loaded into memory. See http://www.cpearson.com/Excel/Classes.aspx
for an introduction to user created classes and objects.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Ross Culver" wrote in message
...
Yeah. I never use objects, only cell/range references and variables. In
fact, if you would and when you have time, briefly explain the why bother
to use an object at all? I need to learn something new today.

Ross

"carlo" wrote in message
...
Thank you all for replying.

@Ross:
the Set is mandatory because copy_rng is an object!

Carlo


On Nov 29, 11:12 am, carlo wrote:
Thanks Basilisk96

I just tried a little bit more and i found out following:
'-------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range("A1:T1") '<--works
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20)) '<--
works not
set copy_rng =
worksheets("sheet2").range(worksheets("sheet2").ce lls(1,1),worksheets("shee-t2").cells(1,20))
'<-- works
'-------------------------------------------------------

i have to give Excel the parent to all the cells!
I thought the cells() in the range object are just for the addresses
sake, but I was wrong, they have to be on the same sheet, so I need to
specify the parentsheet for every cell.

thank you very much, sorry for the confusion.

cheers and thanks

Carlo

On Nov 29, 11:06 am, Basilisk96 wrote:



On Nov 28, 8:52 pm, carlo wrote:

Do I become senile? This thing is driving me crazy!

I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)

Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.

;)
Cheers,
-Basilisk96- Hide quoted text -

- Show quoted text -






JLGWhiz

Set Range.....am I to blind?
 
Hi Ross, If you only run short macros for the active sheet, you probably
won't need to set object variables very much. But, if you are running
complex macros involving two or more sheets and two or more workbooks, then
setting object variables saves time creating the macro, improves efficiency
in running the macro and saves space in storing the macro.

"Ross Culver" wrote:

Yeah. I never use objects, only cell/range references and variables. In
fact, if you would and when you have time, briefly explain the why bother to
use an object at all? I need to learn something new today.

Ross

"carlo" wrote in message
...
Thank you all for replying.

@Ross:
the Set is mandatory because copy_rng is an object!

Carlo


On Nov 29, 11:12 am, carlo wrote:
Thanks Basilisk96

I just tried a little bit more and i found out following:
'-------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range("A1:T1") '<--works
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20)) '<--
works not
set copy_rng =
worksheets("sheet2").range(worksheets("sheet2").ce lls(1,1),worksheets("shee-t2").cells(1,20))
'<-- works
'-------------------------------------------------------

i have to give Excel the parent to all the cells!
I thought the cells() in the range object are just for the addresses
sake, but I was wrong, they have to be on the same sheet, so I need to
specify the parentsheet for every cell.

thank you very much, sorry for the confusion.

cheers and thanks

Carlo

On Nov 29, 11:06 am, Basilisk96 wrote:



On Nov 28, 8:52 pm, carlo wrote:

Do I become senile? This thing is driving me crazy!

I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)

Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.

;)
Cheers,
-Basilisk96- Hide quoted text -

- Show quoted text -






Basilisk96

Set Range.....am I to blind?
 
why bother to
use an object at all?


When writing complex code that spans multiple modules, classes, and
even VBA projects, objects make it easy to pass information between
those entities with the greatest of ease because you can pass the
objects around as arguments without losing data. It's a basic concept
in OOP.

If your code is short, simple, and resides all within one module then
you can get away without having to handle such ominous objectified
monstrosities.

Cheers,
-Basilisk96

Ross Culver[_2_]

Set Range.....am I to blind?
 
Thanks, guys. While I have had to write some lengthy code to handle a
series of conditions or workflow steps, I've never needed to do any really
complex coding nor work with multiple workbooks. I've always tried to
encourage the use of Excel as a reporting tool with an SQL data source, not
as a database; and in my industry (pipe, valve & flanges) there aren't many
complicated calculations.

Still, I'll keep this in mind for the future and do the research suggested
by Chip.

Thanks, again for your time.

Ross


"Ross Culver" wrote in message
...
Yeah. I never use objects, only cell/range references and variables. In
fact, if you would and when you have time, briefly explain the why bother
to use an object at all? I need to learn something new today.

Ross

"carlo" wrote in message
...
Thank you all for replying.

@Ross:
the Set is mandatory because copy_rng is an object!

Carlo


On Nov 29, 11:12 am, carlo wrote:
Thanks Basilisk96

I just tried a little bit more and i found out following:
'-------------------------------------------------------
dim copy_rng as range
set copy_rng = worksheets("sheet2").range("A1:T1") '<--works
set copy_rng = worksheets("sheet2").range(cells(1,1),cells(1,20)) '<--
works not
set copy_rng =
worksheets("sheet2").range(worksheets("sheet2").ce lls(1,1),worksheets("shee-t2").cells(1,20))
'<-- works
'-------------------------------------------------------

i have to give Excel the parent to all the cells!
I thought the cells() in the range object are just for the addresses
sake, but I was wrong, they have to be on the same sheet, so I need to
specify the parentsheet for every cell.

thank you very much, sorry for the confusion.

cheers and thanks

Carlo

On Nov 29, 11:06 am, Basilisk96 wrote:



On Nov 28, 8:52 pm, carlo wrote:

Do I become senile? This thing is driving me crazy!

I recall, I had this problem once...
You're fine, you just need some time AFK for a fresh perspective ;-)

Think about this, and you will see the light, I promise:
Who is the parent of your Cells() calls? That's right, qualify them
explicitly, and it will work in all cases.

;)
Cheers,
-Basilisk96- Hide quoted text -

- Show quoted text -








All times are GMT +1. The time now is 02:07 PM.

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