+ Reply to Thread
Results 1 to 17 of 17

Improving a macro, so that it either hides, or unhides all the columns

  1. #1
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Improving a macro, so that it either hides, or unhides all the columns

    I have the below macro:

    Please Login or Register  to view this content.
    If I have columns b:e unhidden, but columns g:j hidden. And run this macro. It will hide B:E, and unhide g:j

    What I want to do, is make it so that it will hide all of the columns: b:e g:j l:o q:t
    and then unhide all of them if I press it again. And if I press it again it will hide them all. So that whatever the prior situation, after pressing the button, all the columns will be either hidden or unhidden simultaneously.

    Does anyone have any idea how to achieve this? thanks very much
    Last edited by 6StringJazzer; 11-03-2015 at 03:21 PM. Reason: fixed code tags

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Hi TMS,

    Thanks for this, but I would like it to also unhide all the columns together when i run the macro for a second time? So I can alternate the columns between hidden and uhidden (Thanks, and sorry for crazily late reply)

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Have you tried it? That's exactly what it does. Note the "bHide = Not bHide"

    Or, a bit shorter but exactly the same functionality:

    Please Login or Register  to view this content.

    Regards, TMS

  5. #5
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Hi TMS. Thanks for your reply, but it really isnt working for me. I am in Excel 2010. I have saved the macro in the attached. And running it once hides all the columns. But running again doesn't seem to unhide them. Does it work for you!? thanks so much TMSBook3.xlsx

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Your original code works for me?

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Hi John. the original code doesn't fulfill my needs, because lets say b:e are hidden, but g:j are unhidden. I want to press a button that will hide both b:e and g:j. And then unhide both when I press it again. Unfortunately my code would unhide b:e but hide g:j. Does that clarify? thanks

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    @davidx: I'm guessing you just copied the code and not the public variable? If that's the case, it wouldn't work.

    @John: yes, the code will work as expected, provided the state of the columns isn't changed manually. If that happens, you may get a situation where the states swap but some columns will be visible when they should be hidden.

    With the public variable, everything works off the last state, True or False.

    Have a look at the sample workbook attached.


    Regards, TMS
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Improving a macro, so that it either hides, or unhides all the columns

    @ TMS - I'm just realizing that. To be honest I was stomped as well. Thanks for the insite.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    You're welcome.

  11. #11
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Hi TMS. 1. you are a genius. 2. Is there no solution to this then?

    For example, lets say I have labelled columns in the attached as quarters full years. Now for whatever reason, I have manually hidden column F. Unfortunately no combination of those macros will give me all (and only) the Full years; or all (and only) the quarters. Is this just unsolvable? thanks so much Hide - Unhide Columns Davidx.xlsm

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Ahhhh ... the old moving goalpost

    Here's a thought ...

    Please Login or Register  to view this content.

    See the attached updated workbook.


    Regards, TMS
    Attached Files Attached Files

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Oh, sorry, keep pressing the new button ...

  14. #14
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Re: Improving a macro, so that it either hides, or unhides all the columns

    Wow TMS, that is really clever stuff, could never have done it myself thanks. If you have a bit of time, is there anyway to improve the macro further so that of the three states it rotates through, it never hides all the columns. So instead of having 3 states. It has only two. i.e. FYs and quarters? If you can't be bothered that's fine too. Thanks very much already!!!!!

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    You're welcome. Thanks for the rep.

    Actually, it's 4 states down to 3 ... None, FY, Quarters, All. But, having established the principle, it's just a case of tweaking the cycle. In the code below, when you first open the workbook, iState will be zero (0). So, on the first press of the button, it will be incremented to 1, which will show just the Quarters; next press, it will increment to 2 and show Full Years; next press, it will increment to 3 and reset to 0 to show all columns.

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    11-13-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    279

    Re: Improving a macro, so that it either hides, or unhides all the columns

    You are an absolute hero! Thank you so much, I have tried for about two years to solve this! thank you; thank you; thank you!!!!!

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Improving a macro, so that it either hides, or unhides all the columns

    You are very kind.

    Again, you're welcome

+ 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. Need Help writing a VBA for a worksheet calculate event that hides/unhides rows
    By cmitc013 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2015, 10:56 AM
  2. Button appeared at top that hides/unhides cells and spreadsheet
    By javacup in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 12-22-2014, 05:37 AM
  3. Rationalizing my VBA code (it hides/unhides rows and then autofits them)
    By David M. in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2014, 01:06 AM
  4. [SOLVED] Macro code that hides or unhides rows in other worksheets
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-25-2012, 04:10 PM
  5. Macro that hides/unhides sheets depending on cell value
    By amelio in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-13-2011, 06:33 AM
  6. Userform that hides/unhides worksheets
    By fecurtis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-20-2008, 09:36 AM
  7. Button hides unhides columns, how?
    By JimH in forum Excel General
    Replies: 3
    Last Post: 04-20-2005, 05: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