+ Reply to Thread
Results 1 to 14 of 14

Toggle Macro Troubleshooting w/ Multiple Sheets

  1. #1
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Toggle Macro Troubleshooting w/ Multiple Sheets

    I have a simple toggle macro that hides/unhides portions of the sheet when activated. I am not using Excel's built-in toggle button for this; I am just using a standard button. To get the toggle functionality, I have several of these statements within the macro:

    Please Login or Register  to view this content.
    This works fine as long as the button and the named range are on the same sheet. However, I cannot get it to work if the range to hide/unhide is on a different sheet from where the button is located. I have tried this code, but it doesn't work:

    Please Login or Register  to view this content.
    What am I doing wrong?

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

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Don't put brackets around the sheet name e.g.
    Worksheets("Sheet1")
    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 Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Sorry, I should have noted that I didn't use that sheet name; I put the brackets around it to represent a customized sheet name. In reality it's more like this on my sheet:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    This worked fine for me:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Hmmmm, not working for me. Here's what my macro looks like in full:

    Please Login or Register  to view this content.
    The first five named ranges - NamedRange1-5 - are all on the first sheet with the button. Those all work.

    NamedRange6 is one the second sheet (here, "Another Sheet"). Adding the period before "Hidden" as you did in your code caused an error; removing the period allows the macro to work, but it doesn't activate NamedRange6 on the second sheet.

    The last four named ranges - NamedRange7-10 - are ranges that should hide whenever the button is pushed, regardless of the state of the toggle. Interestingly, NamedRange10 is on the second sheet ("Another Sheet") and it works here.

  6. #6
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    I just adjusted the "With" section to this, but it's still not working:

    Please Login or Register  to view this content.

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Do you get any error messages?
    Also is "NamedRange6" workbook, or sheet scope?

  8. #8
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    I am currently not getting any error messages, but NamedRange6 is not hiding/unhiding on the second sheet. All of my named ranges are workbook scope.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    In that case there is no need to use the sheet name, you can simply use
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Ok, I feel really dumb right now but I think I discovered the issue. NamedRange6 is included within the scope of NamedRange10, so even though it hides/unhides properly with this code it is immediately negated when this last line runs:

    Please Login or Register  to view this content.
    I'm sorry for all of the trouble, but I appreciate the help! I'll let you know if I have any further issues.

  11. #11
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Speaking of which, is there a way to run VBA code line by line? I feel like I would have caught this error a lot sooner if I could have seen the code work properly until that last line negated everything.

  12. #12
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Glad you figured it out.
    If you put the cursor anywhere in the code you can use F8 to step through line by line.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Quote Originally Posted by hysterical.useless View Post
    Speaking of which, is there a way to run VBA code line by line?
    http://www.cpearson.com/excel/debuggingvba.aspx

  14. #14
    Forum Contributor
    Join Date
    03-24-2012
    Location
    Midwest
    MS-Off Ver
    Office 365
    Posts
    122

    Re: Toggle Macro Troubleshooting w/ Multiple Sheets

    Thanks! Much appreciated.

+ 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. VBA Troubleshooting - Multiple Issues including randomly appearing checkboxes
    By Storm08 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2018, 11:02 AM
  2. [SOLVED] Help Troubleshooting Macro?
    By AmyV1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2017, 04:19 PM
  3. Min/Max Index Match w/ multiple criteria - Troubleshooting
    By JAM7007 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-24-2013, 11:57 AM
  4. Workbook Sharing - Macro Troubleshooting
    By PY_ in forum Excel General
    Replies: 1
    Last Post: 08-22-2012, 01:28 PM
  5. Multiple Instances - troubleshooting
    By Michelle Tolmay in forum Excel General
    Replies: 2
    Last Post: 02-07-2005, 05:06 AM
  6. [SOLVED] Ron de Bruin Copy2 Macro - troubleshooting
    By gizmo in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-18-2005, 11:06 AM
  7. [SOLVED] how do I step thru a macro for troubleshooting ?
    By chrisq in forum Excel General
    Replies: 1
    Last Post: 01-11-2005, 04: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