+ Reply to Thread
Results 1 to 15 of 15

What's the most "stable" way to activate or select a sheet?

  1. #1
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    What's the most "stable" way to activate or select a sheet?

    Hi,

    I have a really newbie question. What is the most consistent error-free "stable" way to refer to sheets and select or activate them? I have various macros that jump the workbook user across sheets.

    Is it better, for example, to refer to a sheet as "Sheet1" or the sheet name (e.g. "HOME")? Is there a functional difference?

    Is there something better to do for selecting or activating a worksheet or range than just writing:
    Please Login or Register  to view this content.
    I ask because I occasionally get the 1004 method of selection failure error message. My macro runs a bunch of code (adding protections, hiding things etc.) when the workbook is opened and macros are enabled. The very last thing it does is jump the user back to a home screen. The Debug highlights the sheet selection for this chunk of code:

    Please Login or Register  to view this content.
    I KNOW the code is not incorrect because it usually runs fine. I can't usually replicate the error but I need the error to never appear because users will get confused.

    Any thoughts on why I get these errors sometimes and how to prevent them? Thank you so much in advance!!!

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: What's the most "stable" way to activate or select a sheet?

    It's best (fastest) not to select or activate worksheets at all. You don't need to select or activate a worksheet object to work with it.

    It's most robust to refer to worksheets by codename - that way if the worksheet tab name is changed, the code will still work.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    Re: What's the most "stable" way to activate or select a sheet?

    Thank so much, Olly!! OK, I will try to only refer to the sheets by the codename rather than the sheet name.

    Unfortunately, I have found it unavoidable to use select and activate? Some formula calculations on a certain page wouldn't work unless I activated the sheet first. And I use code to select a cell of a sheet to jump the user back to where they were before a macro ran (e.g. a macro might do copy/pasting/calculations etc. elsewhere in the workbook). I also use select for ranges often for macros that copy and paste ranges into other ranges. =/

    I really appreciate your help!!!

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: What's the most "stable" way to activate or select a sheet?

    I have found it unavoidable to use select and activate
    That's not correct.



  5. #5
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: What's the most "stable" way to activate or select a sheet?

    I agree with Olly 100% - best practice is to use Code names and generally avoid activating sheets. That being said, you should be able to activate the "HOME" tab the way you are doing it.

    Only things that I can think of that would mess that line of code up are 1: The user has changed the name of the sheet (which can be avoided by using the code name) or 2: The sheet you are trying to select is hidden (and therefore can't be selected).

    Can you post any of the code that is requiring you to make the sheet's active first? I would be interested in seeing what could be changed to avoid that requirement.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  6. #6
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: What's the most "stable" way to activate or select a sheet?

    Hi there!
    Please let me say that even those copy/pasting/calculations are possible without selecting/activating sheets or flickering screens.
    If you want we can take a look at your code if you can upload a sample file (after you get rid of sensible data of course)
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  7. #7
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    Re: What's the most "stable" way to activate or select a sheet?

    Thanks for all your responses!!
    @GeneralDisarray: Thanks! I was wondering that too, but when the worksheet selection error happened the sheet was definitely unhidden and correctly named so I was really mystified =/

    I would love to avoid "activate" and "select" if those are prone to error as you all are suggesting. I just don't know how to avoid it and still do the things that I need the workbook to do - so I am super grateful for any/all advice!

    Desired functions:

    1. To jump users to the top of certain sheets. There might be multiple people using the same workbook file and users may save the workbook while halfway scrolled down to the bottom of a random sheet. Every time they select a sheet, I want them to be at the top so they don't get confused. I'm using this code which is located in every Microsoft Excel Objects Sheet:
    Please Login or Register  to view this content.
    I also used the code that errored on me (in my original post) to select the "HOME" sheet so that every time a user opens the file anew they land on their familiar home sheet instead of whatever random sheet they or someone else had been working in when they save/closed the file the last time.

    2. To jump users across sheets. The workbook has 23 sheets and users may not be Excel savvy. I created buttons/macros so that they can jump from sheet to sheet without looking for the tab. Here's an example of code for one of the "jump" button macros:
    Please Login or Register  to view this content.
    3. I am generating reports that involve copy/pasting of various ranges. I don't know how to avoid selecting the sheet and selecting the range to run the reports. I'm linking to a fraction of the file to demo what I mean (see "Patient Reports" sheet row 326 and hit the Calculate/Refresh button). Here's the beast mess of code (I'm sure I have a ton of inefficiencies/bad practices!) attached to that button:
    Please Login or Register  to view this content.
    I had trouble with the upload function in the message board so I uploaded the demo file to my Google Drive instead so it should be downloadable through this link: https://drive.google.com/file/d/0Bwe...ew?usp=sharing

    THANK YOU so much in advance!!

  8. #8
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    Re: What's the most "stable" way to activate or select a sheet?

    Another issue came up when I tried to replace all worksheet names with the backend names (e.g. "Sheet2") as suggested: now I'm getting Run-time error '438': Object doesn't support this property or method.

    It's getting angry at this code (maybe because it's part of a statement referencing a ListObject?):
    Please Login or Register  to view this content.
    Necessary to stick with the worksheet name for these types of statements?

    Sorry for all the questions!

  9. #9
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: What's the most "stable" way to activate or select a sheet?

    Hi there,
    It is just a start but I hope you get the idea:

    your code

    Please Login or Register  to view this content.
    can be replaced by this alternative (select-free):

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    Re: What's the most "stable" way to activate or select a sheet?

    @bulina2k Awesome, thank you so much for posting this code improvement!!! I will definitely test it out - it looks so much more efficient

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: What's the most "stable" way to activate or select a sheet?

    can be improved to:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    Re: What's the most "stable" way to activate or select a sheet?

    Thanks so much @snb, and again bulina2k! I implemented this code for the above section and it worked great. It's awesome how few lines of code this needs!

    I'm still trying to figure out how to handle some of the rest of the scenarios without using selection but this is an awesome start to work from!! You guys rock!

  13. #13
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: What's the most "stable" way to activate or select a sheet?

    If the only code in a workbook is for navigation, then strip all the code out and save it as a macro free workbook. You can achieve the affect you want by inserting shapes or images of buttons and hyper-linking them to the points in your workbook you wish to navigate to. [CTRl]>K inserts a hyperlink when you have a shape / image selected
    Frob first, tweak later

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: What's the most "stable" way to activate or select a sheet?

    You don't even need any shapes at all.
    A hyperlink can do without:

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-24-2016
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    41

    Re: What's the most "stable" way to activate or select a sheet?

    Thanks for the suggestions re: Hyperlinks, Neil_ & snb! I unfortunately haven't had the best of luck with those. I originally hyperlinked a bunch of images of buttons for the user interface and jumping across sheets, but one weird quirk I found was that if the previous click was selecting something (e.g. a radio button or check box), the hyperlink would not work until the user clicked away to something else. Macros fixed that for me and I've stayed away from hyperlinks ever since.

    What I have done though - in case the .Activate or .Select code goes haywire again for no discernible reason after functioning just fine many times - is to add some error code. Now all my jump code looks something like this:
    Please Login or Register  to view this content.
    I'd really just like to know why those commands are fundamentally so glitchy, but since it just seems like a given limitation I'm trying to avoid the error pop up messages for users with the On Errors (for non-essential instances) and also keeping their use to a minimum with the new code you all showed me above. My ideal given the glitchiness is to only use the .Activate and .Select for my jump buttons - since that won't really screw up any calculations or data. =/

    Thanks again, so much!!

+ 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. [SOLVED] Excel VBA to Activate/Select Cell based on "Found" Range
    By fhill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2016, 04:37 AM
  2. [SOLVED] select, activate, "with", etc. VB operations with tabs and which is more effecient
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-01-2016, 04:57 PM
  3. Code to Activate a sheet 1 [IF] Userform box is "X" if not Activate Sheet 2
    By Invicta084 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-07-2014, 08:50 AM
  4. Cells().Select and .Activate don't "move the camera" to that cell - how do I do this?
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2014, 03:00 PM
  5. [SOLVED] Activate "sorting" feature when worksheets are setup to "password protect" itself
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2012, 09:04 PM
  6. Prevent code in "Sheet Activate" from running when sheet made visible from other macr
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-21-2006, 04:15 AM
  7. Questionnaire sheet: Select "yes" or "no," and only one can be selected
    By bpatterson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-13-2006, 06:10 PM

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