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:
However, it is not working.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
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."
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
I think your problem is with this line:
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.Code:ActiveSheet.Protect Password:="test", userinterfaceonly:=True
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.
Kafrin
The option userinterfaceonly allows macros to run on a protected sheetCode:ActiveSheet.Protect Password:="test", userinterfaceonly:=True
But you are correct
Calling it in this routine is part of the problem.
OK, haven't used the userinterfaceonly parameter before, so that's useful to know. Thanks for the heads-up.
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."
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
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."
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
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
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."
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?
Some coditions set this as False Then the final line sets them back to TrueCode:Columns("U:CQ").Hidden = 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
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."
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.
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."
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
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."
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks