+ Reply to Thread
Results 1 to 22 of 22

Conditionally Hide Columns

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Question Conditionally Hide Columns

    Hi all,

    I am trying to hide all unnecessary columns to aid in the data entry process.

    The data entry section is A4:CY1504.

    For each record, column D would have one of several codes (CM, LR, MG, NR, etc.), and column Q would have one of several reasons (Excessive time charges, Failure to produce documentation, Not an eligible benefit, Overcharged, etc.) - drop-down lists.

    When the user selects/enters either “CM”, “LR”, “MG” or “NR”, I want to hide columns U:AI, AP:BJ and BQ:CQ.

    Then, for the same record, if the user selects/enters:

    “Excessive time charges”, I want to hide columns AM:BP

    “Failure to produce documentation”, I want to hide columns AJ:AL and BK:BP

    “Not an eligible benefit”, I want to hide columns AJ:AO and BN:BP

    “Overcharged”, I want to hide columns AJ:BM

    When the user selects/enters any other code in column D, I want to hide columns U:CQ.

    When the user saves the file or enters anything in column C, I want to unhide all columns.

    So far, I have the following:

    Please Login or Register  to view this content.
    However, it is not working.

    Any help, please?

    Thank you,
    Gos-C
    Last edited by Gos-C; 03-23-2010 at 01:18 PM.
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Problem arrises because your enable events condition is in the wrong place and you have not reset it to true.

    I dont know why you would want to use protect in this routine

    Try this

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    04-16-2009
    Location
    Dorset, UK
    MS-Off Ver
    Office 2003, Office 2007
    Posts
    131

    Re: Conditionally Hide Columns

    I think your problem is with this line:
    Please Login or Register  to view this content.
    You cannot show and hide columns while the sheet is protected. Instead, make sure the sheet is unprotected (use the ActiveSheet.Unprotect command if necessary - you may need to test whether there's any protection first), then show and hide your columns, then protect the sheet.

    Alternatively you could set up the different options as CustomViews and switch between them in code, but you would still need the sheet to be unprotected for the switch.

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Kafrin

    Please Login or Register  to view this content.
    The option userinterfaceonly allows macros to run on a protected sheet

    But you are correct
    Calling it in this routine is part of the problem.

  5. #5
    Forum Contributor
    Join Date
    04-16-2009
    Location
    Dorset, UK
    MS-Off Ver
    Office 2003, Office 2007
    Posts
    131

    Re: Conditionally Hide Columns

    OK, haven't used the userinterfaceonly parameter before, so that's useful to know. Thanks for the heads-up.

  6. #6
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Hi all,

    Thanks for responding.

    My sheet is protected, so I need to unprotect it in order to hide/unhide the columns.

    Gos-C

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Hi

    That should not be the case when using the option UserInterfaceOnly with Protect

    But I had not considered the implications of the user having to input for the macro to trigger

    Macros will run provided they don't require user input through the sheet.

    I am looking at that now - Give me an hour or so and look back then.
    Last edited by Marcol; 03-10-2010 at 12:49 PM. Reason: Pressed the wrong button

  8. #8
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Thank you very much, Marcol. I appreciate it.

    Gos-C

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Hi Gos

    There is a conflict with the sheet protection rules in this project.

    Yes a macro can be run with out having to remove the protection.

    But in this case it can't be triggered because the sheet requires to be changed and that is protected

    I'll need a sample of the actual sheet (with no sensitive data) to see where entry is allowed to solve this.

    Could you please post such a sheet

    Regards
    Alistair

  10. #10
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Hi Marcol,

    I have attached a sample of the file (in Excel 97-2003 Workbook format) - I am using Excel 2007. The sheet protection password is "test". Thanks for you help.

    Gos-C
    Attached Files Attached Files

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Hi Gos

    Check this file out. I have an issue with some bits of your code. Mainly with what columns to hide.

    There seems to me that there are some conflicting overlaps

    What should this end up as True or False?
    Please Login or Register  to view this content.
    Some coditions set this as False Then the final line sets them back to True
    Overlaps on "CQ"?

    I have also tied all the conditions to the active row, I think that is correct.

    Don't worry about protection just now let's get the code correct first.

    Got to go now but I'll look in when I get back in a few hours time
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Hi Allistair,

    I am leaving the office now (I have to pick my wife up), so I will review your comments when I get home and then get back to you (tonight or tomorrow) all's well.

    Thank you,
    Gos

  13. #13
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Hi Marcol,

    Yes, all conditions should be tied to the active row.

    Columns U:CQ are not requires for data entry when code CR, GN, PA or SC is entered.
    Also, I had missed two other Columns:

    S:T are only required when code CR, GN, PA or SC is entered. Therefore, they should be hidden when code CM, LR, MG or NR is entered.

    (See the modified code.)

    The first part of the code is working but the “Select Case Range (“Q”& Target.Row).Value” is not working. Can you give it another try?

    I added a command button to unhide the columns when requires (for viewing the data). Would that be necessary? Also, I modified the Data Validation in column Q.

    Thank you very much for you time.
    Attached Files Attached Files

  14. #14
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    No Problem

    I'll finish it asap. Can't do it right away.

    Don't know what part of Canada you're in so I could be between 5 & 8hrs "ahead" of you.(Check GMT)

    I'll PM you as soon as I post again

    Cheers

  15. #15
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    I am in Toronto, Ontario

  16. #16
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Hi Gos

    Sorry about the delay. Got called away.

    Try this out.

    I don't know what cells to lock and what your list drop downs rules are (I may have lost some while testing)

    1/. I have added column "Q" to the macro activation, it should hide the columns based on "D" in the same row, as does column "D" with "Q".

    Don't know if you need this, but it seemed logical to me. It is fairly obvious how to cancel this.

    2/. I have made your new button toggle hide/unhide columns "S:CQ"

    3/. The sheet will be protected when it is opened. To make changes to the sheet use unprotect (Password = "test") don't protect it again. Save/Close/Re-open to check changes. Same with changing the password in ThisWorkbook

    4/. You should also Password protect the VBa to prevent any adventurous user going into the code and finding sheet Password.

    Let me know how you get on.

    Regards
    Alistair
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Thumbs up Re: Conditionally Hide Columns

    Looks good! Thanks a million!

    I will test it thoroughly and get back to you later.

    Gos-C

  18. #18
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Hi Marcol,

    I had some users test the sheet and they found that I made a few errors in parts of the logic.

    I modified the code a bit, but I can’t get it to work like it should. If you can help me fix it, that would be great.

    1. The Toggle Columns button should hide/unhide column Q together with columns U:CQ. Note: Columns S:T must always be shown when the toggle button is used. (Can you please verify that I coded it correctly.)

    2. For column D, there should be three cases – each with different columns being hidden: (1) CM; (2) LR, MG and NR; and (3) CR, GN, PA and SC. (Can you please verify that I modified the code correctly.)

    3. Columns S:T should be hidden when CM, LR, MG or NR is selected, and unhidden when any other code is selected. (Can you please verify that I modified the code correctly.)

    4. If the user deletes the contents of a cell in column D, the contents of the cells in columns Q and S:CQ for that row should be deleted, and all columns unhidden.

    5. Instead of:
    ‘if delete is used (in column Q) then unhide all columns and exit,

    can it be:

    ' if delete is used (in column Q) then unhide the columns that were hidden (in other words, show the columns that were shown) when that selection was originally made and exit

    In other words, when the user deletes the contents of a cell in column Q, the macro should unhide the columns that were hidden when that selection was made – effectively, reverting to the columns that were shown when the log code in column D (for that row) was selected. For example, if the user selects LR in column D, and “Exceeds plan limit” in column Q, but later deletes ”Exceeds plan limit” in column Q, the columns that were hidden when “Exceeds plan limit” was selected, should be unhidden.

    Also, can you please explain “isect”? It’s the first time I am seeing it in macros.

    Thanks again for your help.

    Regards,
    Gos-C
    Attached Files Attached Files

  19. #19
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    1/. The Toggle Columns button should hide/unhide column Q together with columns U:CQ.
    Changes
    I have added "Q" to the if statement to define the exact condition to toggle under.
    I have changed the Toggle Button caption to suit.

    2/. For column D, there should be three cases
    Changes
    None - Your code modification is fine.

    3/. Columns S:T should be hidden when CM, LR, MG or NR is selected, and unhidden when any other code is selected.
    Changes
    I have added Columns("S:T").Hidden = False to Case "CR", "GN", "PA", "SC" to fullfill the unhidden condition.

    4/. If the user deletes the contents of a cell in column D, the contents of the cells in columns Q and S:CQ for that row should be deleted, and all columns unhidden.
    Changes
    I have added code to handle this condition. It will require testing to cover possible user situations I am unaware of
    Note the use of Application.EnableEvents = False/True in this additional code.

    5/. Instead of: ‘if delete is used (in column Q) then unhide all columns and exit,.........
    Changes
    I have added code to handle this condition.
    Code in 4/. will override this on occassions , this is unavoidable, but it is probably correct to do so.

    In most of the above, I cannot say for sure that the result is correct, I cannot verify the required conditions, only you can do this.
    The methods used, however, should put you on the right track.

    6/. Also, can you please explain “isect”? It’s the first time I am seeing it in macros.
    This is the variable name Set by the Intersect function.
    In this line
    Please Login or Register  to view this content.
    Highlight Intersect press f1 to see an explaination by microsoft

    In my experience Worksheet_Change is seldom used without it.

    Help in Excel VBa is invaluable it contains a mine of information. Use it!!!

    I have attached an amended workbook for your evaluation.

    Please let me know how you get on with it.

    Regards
    Alistair

    If this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody!
    Also
    If you are satisfied by any members response to your problem please consider using the scales icon top right of thier post to show your appreciation.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Conditionally Hide Columns

    Thank you, Marcol. I have to run out to clear my pool table at Canada Customs so I will get back to you later.

    Gos-C

  21. #21
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Thumbs up Re: Conditionally Hide Columns

    Hi Marcol,

    It is working fine now. Thank you very much for you help. I greatly appreciate it.

    Kindest regards,
    Gos-C

  22. #22
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Conditionally Hide Columns

    Glad to have been of some help

    Enjoy your pool ....... with beer?.....

    Cheers
    Alistair

    If this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody!
    Also
    If you are satisfied by any members response to your problem please consider using the scales icon top right of thier post to show your appreciation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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