+ Reply to Thread
Results 1 to 12 of 12

Confusion about Scope of workbooks and worksheets

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    180

    Confusion about Scope of workbooks and worksheets

    I fell unsure about understanding the Scope of workbooks and worksheets

    Context of question:
    Within one module associated with a single workbook
    Within a single subroutine
    If I open an existing workbook and existing worksheet within the open workbook
    assigning a variable to each of the above objects

    then make a function call within the above single subroutine
    I am assuming the code inside the function call
    Has no idea which workbook or worksheet is open

    Hence I assume I must pass the variable name for the workbook and
    worksheet as parameters to the function call and use them to extract
    data from the worksheet

    Question:
    Do I understand the scope of workbooks and worksheets within a single subroutine?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Confusion about Scope of workbooks and worksheets

    Quote Originally Posted by whburling View Post

    Hence I assume I must pass the variable name for the workbook and
    worksheet as parameters to the function call and use them to extract
    data from the worksheet

    Not necessarily.

    If in the function you reference a Range and don't pass the workbook and worksheet variables, the Range defaults to whatever the Active sheet is at the time the function is called. The code could be in any workbook. An unqualified Range within that code defaults to the currently active sheet.

    If you assign a variable to a worksheet, that variable includes the parent workbook the sheet is belongs to. So by referencing that variable, the code also knows its parent workbook. If you were to pass a worksheet variable to a function, the code knows what workbook that sheet belongs to.

    The same hods true for a range variable, If you assigned a Range to a variable, that variable includes its parent sheet and workbook as well.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Confusion about Scope of workbooks and worksheets

    Hi whburling- All open workbooks are part of the Workbooks Collection, which is accessible from any procedure in any workbook (VBAproject). You can refer to them by:
    1) Set WB = Workbooks(2) , if you know their current positions in the list (careful, this varies)
    2) Set WB = Workbooks("MyWorkbook.xlsx") , if you know the name

    The keywords ActiveWorkbook (the active book) and ThisWorkbook (the book containing your VBA code) are also useful.
    3) Set WB = ActiveWorkbook
    Workbooks("SomeOtherWorkbook.xlsx").Activate


    Worksheets work in similar fashion, with each workbook having it's own Worksheets Collection
    4) Set WS = Workbooks("SomeOtherWorkbook.xlsx").Worksheets("Sheet1")
    Set MyRange = WS.Range("A1")
    MyRange.Value = "Hello"
    MyRange is fully qualified. (Its parent workbook and worksheet are part of the definition)

    Using these methods, you can freely access any cells in any open workbook. Just remember, any unqualified range references are assumed to be on the Active Worksheet in the Active Workbook.

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 04-25-2017 at 05:33 PM.

  4. #4
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    180

    Re: Confusion about Scope of workbooks and worksheets

    First....Thank you so much for responding. I am very grateful.

    ok....so this is what I put together in my mind from reading your message to me:

    * there is a hierarchy of objects: workbook includes worksheets which includes range
    * initially range must define the worksheet it is a part of AND
    * initiallu a worksheet must define a workbook in which it was defined

    Interestingly, each object knows who its parent is and who its children are! I have imagined that the parent only knew its children but the children did not know who its parents were!!!
    and so whenever any of the above objects are used, the parents (workbook -> worksheet) are known

    now....while the default active workbook may be know....and the default active worksheet may be known....i am opening myself up to problems if multiple workbooks are open.

    Hence I feel I want to define the workbook or worksheet i will be working on instead of just using "activeWorkbook or Thisworkbook.

    So...do i do that by passing the worksheet name to the function or subroutine...and just continue using that worksheet name inside of the function or subroutine? If so, i would love to pass just the pointer to that worksheet adn not the whole object. how do i do that?

    Secondarily, if i am worried that the worksheet might have an identical worksheet in another workbook, perhaps I should pass the workbook variable (or pointer) as well?

    what do you do? i do not have a feel for how specific I should be

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Confusion about Scope of workbooks and worksheets

    Hi,

    Any object you pass will always be passed as a pointer. If you pass a worksheet object, you do not need to pass a workbook as well, since a worksheet object always refers to a specific sheet in a specific workbook.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Confusion about Scope of workbooks and worksheets

    You can also pass workbook and/or worksheet names as strings from one procedure to another, and let them instantiate those objects internally.
    Last edited by leelnich; 05-06-2017 at 11:45 PM.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Confusion about Scope of workbooks and worksheets

    Hi,

    Here is my best link to explain Scope:
    http://www.cpearson.com/excel/Scope.aspx

    In VBA it is also very important to know about the With and using the period in front of ranges. ".Range"
    http://www.informit.com/articles/art...21718&seqNum=5
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Confusion about Scope of workbooks and worksheets

    You can indeed pass the names, as long as you pass both workbook and worksheet.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Confusion about Scope of workbooks and worksheets

    Quote Originally Posted by whburling View Post
    Secondarily, if i am worried that the worksheet might have an identical worksheet in another workbook, perhaps I should pass the workbook variable (or pointer) as well?

    what do you do? i do not have a feel for how specific I should be
    My preference would be to pass the worksheet or workbook objects (pointers).

    Example: Prompts the user to open a file. Then it passes the worksheet to a sub procedure.

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 04-26-2017 at 03:36 AM.

  10. #10
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Confusion about Scope of workbooks and worksheets

    When there are multiple workbooks open (or multiple worksheets that might be active) it is common to make the desired workbook and/or worksheet active and then take an action, as opposed to "staying where you are" (in terms of which workbook or worksheet is active) and specifying which workbook or worksheet to act upon by prefixing. E.g. you might go
    Please Login or Register  to view this content.
    as opposed to
    Please Login or Register  to view this content.
    It's a theoretical tradeoff. On the down side, Activating first and not prefixing can
    - have a small extra amount of CPU processing (which might build up to a significant amount depending on the scenario)
    - maybe be distracting since the display changes (perhaps even annoyingly flicker)
    - It is not as explicit as specifying the object.

    However if those are not of concern (which is often the case) then not having to prefix operations might make the code easier to follow. By going wbSales.Activate you might avoid typing
    wbSales.
    9 times in the next several lines of code. One benefit of the resulting "compactness" might be that some lines of code don't extend off the right side of the screen, or require underscore line continuation, in order to see all code without scrolling. You or another code maintainer may also be able to more quickly mentally process what code is doing when removing the "distraction" of the prefixes. However, it's still a tradeoff. Explicitly saying wbSales. would in some cases actually make the code easier to read and maintain.

    If performance and readability/maintainability are not a big factor, I often just Activate the workbook or worksheet of concern first just because I personally find it easier to mentally follow along. At least it's much easier for me during initial code development to have the object active so I can quickly toggle alt-F11 to see what's happening, even if later on I add in prefixes and remove Activate lines.

    Now having said all that I still may have to do the prefix
    ActiveSheet.
    despite all my "lazy" efforts to avoid having to prefix operations. Again, it's a tradeoff such that different projects may call for .Activate and others might not. I'm just pointing out that there are two ways to go.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Confusion about Scope of workbooks and worksheets

    Quote Originally Posted by Oppressed1 View Post
    By going wbSales.Activate you might avoid typing
    wbSales.
    9 times in the next several lines of code.
    A With block would also avoid that need.

  12. #12
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Confusion about Scope of workbooks and worksheets

    Quote Originally Posted by xlnitwit View Post
    A With block would also avoid that need.
    Derrr! Thanks for reminding me, because With is always a distant afterthought with me for some reason.
    Chip, if you can read this, you're STILL making the world a better place with your immeasurably valuable gifts. Every week, you continue to help me (and untold numbers of others) tremendously. Practically no alternative outlets can match your effectiveness, even over the vast number of topics you demystify and empower usage of. You were, and still are, amazing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Moving worksheets from workbooks in one folder to workbooks in other folders of same name.
    By Mohammadhafiz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-30-2016, 01:00 PM
  2. Scope NamedRange to Multiple WorkSheets
    By ptmuldoon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2015, 07:56 PM
  3. [SOLVED] copy worksheets from a couple of workbooks to other workbooks
    By masben in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-30-2014, 02:56 PM
  4. [SOLVED] Copying Worksheets from Multiple Workbooks to a Single Workbook, Separate Worksheets
    By DHartwig35805 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-27-2012, 11:38 AM
  5. Copying Worksheets from Multiple Workbooks to a Single Workbook, Separate Worksheets
    By Abhi_1977 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-27-2012, 11:32 AM
  6. [SOLVED] LINKED WORKBOOKS - TOTAL CONFUSION
    By DR Hall in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-29-2005, 08:45 AM
  7. Scope and Lifetime confusion.
    By hanjohn@netspace.net.au in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-07-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1