+ Reply to Thread
Results 1 to 8 of 8

WHY Activate a Worksheet?

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    226

    WHY Activate a Worksheet?

    OK, this is probably a stupid-simple question, but WHY do you ever need to activate a worksheet (if you have assigned a variable name to that sheet and can refer to it using that name)?

    The reason I'm asking: I'm trying to simply set the value of a named range (single cell, call it "OtherCell") on one worksheet (that I have set as "OtherSheet") equal to the value of another single-cell named range (call it "SourceCell") which is in ThisWorkbook. NOTE that OtherSheet is in a different (open) workbook (not ThisWorkbook). So I wrote something like this:
    Please Login or Register  to view this content.
    I get a runtime error 424 (object required) whenever I run this command. If, however, I simply go look at OtherSheet (making it the active sheet), then the command runs fine. So apparently (I'm guessing), I can't change a value in another workbook (other than ThisWorkbook) unless I first activate that worksheet (or workbook?).

    So before I go edit all my code, I'm trying to understand:
    • When do I have to activate a (not ThisWorkbook) worksheet or workbook when running a macro?
    • What can (and can't) I do to or with the "other" worksheet if it's not activated?
    • Can I read / copy data from another workbook without activating it?
    • Can I sort data in another workbook without activating it?
    • If I activate the "other" workbook or worksheet, do I then need to re-activate ThisWorkbook before I can make changes to it (via the macro)?

    I understand that this OUGHT to be a very basic topic, but all I can find in any documentation is HOW to activate a worksheet. No one ever seems to explain WHY / WHEN you need to activate a worksheet! So if someone can please enlighten me, I'd appreciate it. Thanks!

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: WHY Activate a Worksheet?

    You hardly ever need to activate a worksheet (or select any cells etc)!
    You can copy, paste, delete columns without activating any worksheets
    You can attribute values without activating
    And you can do all of that within one worksheet, within one workbook , across multiple workbooks

    Put both attached files in the same folder
    Open File002 and look at the value of cell A1
    Now close it
    Open File001 and run macro below with {CTRL}K
    Open file002 and check value in A1
    Nothing has been activated in the code but the value in A1 has changed

    Please Login or Register  to view this content.
    Whatever preconceptions you have, ditch them and start again with an open mind
    Attached Files Attached Files
    Last edited by kev_; 04-08-2018 at 03:47 PM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,497

    Re: WHY Activate a Worksheet?

    The problem is you work from a lot assumptions of how you think it works and not follow the vba logic..
    in your code you reference "othersheet.[othercell] to us in words you tell that othersheet refers to a different workbook.
    So tell in your code it is in another workbook..

    if you want to reference another workbook you need something like
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    most likely you also want to check if that other workbook is open to avoid code failing if you forgot to open
    Last edited by Roel Jongman; 04-08-2018 at 03:19 PM.

  4. #4
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    226

    Re: WHY Activate a Worksheet?

    OK, mistake on my part...

    Went back and stepped through things again. After I open the file with OtherSheet in it (I have a variable OtherSheet declared As Range, and I set it to refer to one of the sheets in the newly opened file), it turns out that my statement:
    Please Login or Register  to view this content.
    actually DOESN'T work (I get the Error 424) when OtherSheet is active. It only works if ThisWorkbook is active.

    I had thought that unqualified range name references (like [SourceCell]) would always refer to ThisWorkbook, but apparently that is not the case!

    So... is the bottom line: To use any range name, you must either qualify it to the workbook (or worksheet) that it resides in, OR that workbook must be the active workbook? Is that correct?
    Or to put it from another perspective, an unqualified range name reference is assumed to refer to ActiveWorkbook, NOT ThisWorkbook?

  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: WHY Activate a Worksheet?

    Quote Originally Posted by Merf View Post
    Or to put it from another perspective, an unqualified range name reference is assumed to refer to ActiveWorkbook, NOT ThisWorkbook?
    That is correct, assuming the code is not in an object module that has a Range property. Your code is really calling Evaluate rather than Range, but the same principle applies.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: WHY Activate a Worksheet?

    Beware of this twist

    STANDARD module vs SHEET module
    If the code is in a standard module - unqualified ranges refer to the active sheet
    If the code is in a specified sheet module - unqualified ranges refer to the specified sheet

    Example
    One workbook, 2 sheets ("Data" and "Values") and sheet "Values" is the current ACTIVE sheet


    If the code is in a STANDARD module
    ...Range("A10") refers to sheet "Values" (ie the active sheet)

    If the code is in the SHEET module for sheet "Data"
    ...Range("A10") refers to sheet "Data" (even though "Values" is active)

    If the code is in the SHEET module for sheet "Values"
    ...Range("A10") refers to sheet "Values" (but not because it is the active sheet)

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: WHY Activate a Worksheet?

    Could the problem be the use of the []?
    If posting code please use code tags, see here.

  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: WHY Activate a Worksheet?

    Quote Originally Posted by kev_ View Post
    If the code is in a standard module - unqualified ranges refer to the active sheet
    Unless it's a named range, in which case it refers to the active workbook. In a worksheet code module, it's effectively Worksheet.Range whereas in any other module, it's Application.Range.

+ 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. Activate this worksheet
    By cmb80 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-27-2014, 12:11 PM
  2. [SOLVED] VBA to activate the other worksheet
    By impresxy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2013, 04:38 AM
  3. Replies: 3
    Last Post: 10-10-2012, 05:10 PM
  4. Worksheet Change and Worksheet Activate Events Reprotecting Automatically
    By excelnewb02 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-21-2012, 08:44 PM
  5. Activate worksheet
    By jwongsf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2009, 09:11 AM
  6. Replies: 3
    Last Post: 10-18-2005, 08:05 PM
  7. Activate Worksheet
    By cottage6 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2005, 02:06 PM

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