Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default workbook or worksheets

<a
href="http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.public.excel.programming &mid=127739c5-cb43-423a-b963-855688533e0f&sloc=en-us"Earlier</a
I had asked about a Range problem. Eventually, I played around and solved it
simply by moving the macro from being associated to one of the worksheets to
being associated with "This Workbook."
I don't understand why this made a difference since I was referred to
specific worksheets in the code.
This begs a few questions:
1) Is it better form to associate a macro with a specific worksheet or to
the workbook?
2) If I'm associating the macor to a worksheet what are the limitations?
Thanks,
David
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default workbook or worksheets

I am going out on a limb here and assuming that your range problem was a
named range problem? Named ranges are somewhat odd in that they can exits at
the worksheet level or at the borkbook level. Try this. Create a worksheet
with a named range on it. Now copy the worksheet. You now have two named
ranges both with the same name. The workbook version has presidence over the
worksheet version. You can confirm that for yourself by Hitting F5. Only one
of your named ranges is listed. The other is a cell range reference. That is
probably where the problem stemmed from. Assuming named ranges...

Is it better to associate code with a workbook or with a worksheet. That
depends on the scope of the function or procedure. If the function is
specific to one sheet then put it in the sheet. If the function is more
utilitarian than that and could be used by multiple sheets then put it in a
module. That is a little oversimplified but simple is good.
--
HTH...

Jim Thomlinson


"David Gerstman" wrote:

<a
href="http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.public.excel.programming &mid=127739c5-cb43-423a-b963-855688533e0f&sloc=en-us"Earlier</a
I had asked about a Range problem. Eventually, I played around and solved it
simply by moving the macro from being associated to one of the worksheets to
being associated with "This Workbook."
I don't understand why this made a difference since I was referred to
specific worksheets in the code.
This begs a few questions:
1) Is it better form to associate a macro with a specific worksheet or to
the workbook?
2) If I'm associating the macor to a worksheet what are the limitations?
Thanks,
David

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default workbook or worksheets



"Jim Thomlinson" wrote in message
...
The workbook version has presidence over the
worksheet version.


Not always Jim. When on the worksheet that the worksheet nameis defined in,
the worksheet name takes precedence.

IMO, having the same name on different worksheets is confusing, but can be
useful. Having a worksheet name and a workbook name with the same name is
just asking for problems.

As an aside, see http://www.xldynamic.com/source/xld.Names.html


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default workbook or worksheets

True enough on the name thing. The scope of the name is similar to a global
variable. It takes presidence unless a local variable by the same name is
also defined at which point the local name takes presidence. I was alluding
to the specific question asked where the global name was taking presidence.
Upon re-reading my answer it sure does not come across that way. Thanks for
clarifying...

--
HTH...

Jim Thomlinson


"Bob Phillips" wrote:



"Jim Thomlinson" wrote in message
...
The workbook version has presidence over the
worksheet version.


Not always Jim. When on the worksheet that the worksheet nameis defined in,
the worksheet name takes precedence.

IMO, having the same name on different worksheets is confusing, but can be
useful. Having a worksheet name and a workbook name with the same name is
just asking for problems.

As an aside, see http://www.xldynamic.com/source/xld.Names.html



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default workbook or worksheets

Not sure why Jim turned this into a discussion of range names, but based on
your reference,
Your code was this:
Sub test3()
Dim str As String
str = Worksheets("Commands").Cells(2, 4)
Dim emp_range As Range
Set emp_range = Worksheets(str).Range("a1", Range("a2").End(xlDown))
For Each c In emp_range
MsgBox c.Value
Next c
End Sub

In a sheet module this is your problem - you are referring to ranges on two
different sheets in this line

Set emp_range = Worksheets(str).Range("a1", Range("a2").End(xlDown))

Thus your error.

in a sheet module, the unqualified Range("a2") refers to the sheet
containing the code - but str used in the qualification of the first range
is probably not the sheet containing the code. (also str is an excel
function, so using it as a variable is not wise) .

? typename(str(1233))
String
converts a number to a string

When you move it to the thisworkbook module Apparently it isn't a problem.
Nonetheless, as a general rule, all your code should be in general modules
(insert=Module). Class modules such as Sheet modules and the Thisworkbook
module should be reserved for events associated with those objects. In
those cases, you can have excel enter the declaration from selecting the
object in the left dropdown at the top of the module and the event from the
right dropdown at the top of the module. Certainly this is a matter of
personal style, but I would highly recommend it to avoid problems like you
had.

--
Regards,
Tom Ogilvy


"David Gerstman" wrote in message
...
<a

href="http://www.microsoft.com/office/community/en-us/default.mspx?dg=micros
oft.public.excel.programming&mid=127739c5-cb43-423a-b963-855688533e0f&sloc=e
n-us"Earlier</a
I had asked about a Range problem. Eventually, I played around and solved

it
simply by moving the macro from being associated to one of the worksheets

to
being associated with "This Workbook."
I don't understand why this made a difference since I was referred to
specific worksheets in the code.
This begs a few questions:
1) Is it better form to associate a macro with a specific worksheet or to
the workbook?
2) If I'm associating the macor to a worksheet what are the limitations?
Thanks,
David





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default workbook or worksheets


Thank you. It must have been using str as a variable. How stupid of me.
Thank you.
David
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
Combine worksheets in multiple workbook in one workbook with a macro Sam Commar Excel Discussion (Misc queries) 2 April 2nd 09 01:09 PM
Can't add worksheets to a workbook. MCorn Excel Discussion (Misc queries) 1 August 22nd 08 10:24 PM
Create New Workbook from Worksheets in One Workbook Dave Excel Discussion (Misc queries) 4 June 17th 07 10:27 PM
How do I build a workbook from the worksheets another workbook? Rico Excel Discussion (Misc queries) 4 August 19th 05 02:04 PM
Link multiple worksheets in one workbook to another workbook and . HeatherCarr Excel Programming 0 March 28th 05 10:35 PM


All times are GMT +1. The time now is 11:47 PM.

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

About Us

"It's about Microsoft Excel"