+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Valued Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    343

    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:

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Target.Column <> 4 Then Exit Sub 'only works in Columns D
            If Target.Count > 1 Then Exit Sub
                If Intersect(Target, Cells(4, 4)) Is Nothing Then Exit Sub
    ActiveSheet.Protect Password:="test", userinterfaceonly:=True
    Cells.Columns.Hidden = False
    Select Case Cells(4, 4).Value
    Case "CM", “LR”, “MG”, “NR”
       Columns("U:AI").Hidden = True
       Columns("AP:BJ").Hidden = True
       Columns("BQ:CQ").Hidden = True
    End Select
    Select Case Cells(4, 17).Value
    Case "Excessive time charges"
       Columns("AM:BP").Hidden = True
    Case "Failure to produce documentation"
       Columns("AJ:AL").Hidden = True
       Columns("BK:BP").Hidden = True
    Case "Not an eligible benefit"
       Columns("AJ:AO").Hidden = True
       Columns("BN:BP").Hidden = True
    Case "Overcharged"
       Columns("AJ:BM").Hidden = True
    Case Else
        Columns("U:CQ").Hidden = True
    End Select
    End Sub
    However, it is not working.

    Any help, please?

    Thank you,
    Gos-C
    Last edited by Gos-C; 03-23-2010 at 02:18 PM.
    Using Excel 2007 & 2010/Windows XP

    "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 Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,138

    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

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column <> 4 Then Exit Sub 'only works in Columns D
        If Target.Count > 1 Then Exit Sub
        If Intersect(Target, Cells(4, 4)) Is Nothing Then Exit Sub
             
        Application.EnableEvents = False
                    
        'ActiveSheet.Protect Password:="test", userinterfaceonly:=True
        Cells.Columns.Hidden = False
        Select Case Cells(4, 4).Value
        Case "CM", “LR”, “MG”, “NR”
           Columns("U:AI").Hidden = True
           Columns("AP:BJ").Hidden = True
           Columns("BQ:CQ").Hidden = True
        End Select
        Select Case Cells(4, 17).Value
        Case "Excessive time charges"
           Columns("AM:BP").Hidden = True
        Case "Failure to produce documentation"
           Columns("AJ:AL").Hidden = True
           Columns("BK:BP").Hidden = True
        Case "Not an eligible benefit"
           Columns("AJ:AO").Hidden = True
           Columns("BN:BP").Hidden = True
        Case "Overcharged"
           Columns("AJ:BM").Hidden = True
        Case Else
            Columns("U:CQ").Hidden = True
        End Select
        Application.EnableEvents = True
    End Sub

  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:
    Code:
    ActiveSheet.Protect Password:="test", userinterfaceonly:=True
    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 Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,138

    Re: Conditionally Hide Columns

    Kafrin

    Code:
    ActiveSheet.Protect Password:="test", userinterfaceonly:=True
    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
    Valued Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    343

    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
    Using Excel 2007 & 2010/Windows XP

    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

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

    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
    Valued Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    343

    Re: Conditionally Hide Columns

    Thank you very much, Marcol. I appreciate it.

    Gos-C
    Using Excel 2007 & 2010/Windows XP

    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

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

    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
    Valued Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    343

    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
    Using Excel 2007 & 2010/Windows XP

    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

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

    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?
    Code:
    Columns("U:CQ").Hidden = True
    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
    Valued Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    343

    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
    Using Excel 2007 & 2010/Windows XP

    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

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

    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
    Using Excel 2007 & 2010/Windows XP

    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

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

    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
    Valued Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    343

    Re: Conditionally Hide Columns

    I am in Toronto, Ontario
    Using Excel 2007 & 2010/Windows XP

    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

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.2.0