Automatically hide multiple columns in one worksheet based on multiple inputs on another

AlexCross

New Member
Joined
Jun 29, 2019
Messages
4
Hello,

My employees work on multiple projects, they use excel to record their hours. We currently have 10+ projects but an employee may only be working on any 5 of those. Each employee has their own excel spreadsheet (stored on sharepoint- online 365)

I would like to be able to automatically hide columns /projects(3 columns per project) on sheet 2, that the employee is not working when the value under the Assigned heading on Sheet 1 (see C2-C13 etc) for a project is "N". Column C has a data validated function to be either "blank", "Y" or "N"

Sheet 1 below

[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD][/TD]
[TD]Notes:corresponding column on sheet 2 to hide[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Employee:[/TD]
[TD]John Smith[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]Project Name[/TD]
[TD]Assigned [/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]T500[/TD]
[TD]Y[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]T501[/TD]
[TD]N[/TD]
[TD][/TD]
[TD]F-H[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]T502[/TD]
[TD]Y[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD]T503[/TD]
[TD]N[/TD]
[TD][/TD]
[TD]L-N[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD]T504[/TD]
[TD]Y[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD]T505[/TD]
[TD]Y[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD]T506[/TD]
[TD]N[/TD]
[TD][/TD]
[TD]U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
[TD][/TD]
[TD]ETC[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Date[/TD]
[TD][/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Can you please assist me with appropriate coding to complete this function, your assistance is greatly appreciated.

Alex

The result of the coding would be as follows:
[TABLE="class: grid, width: 200"]
<tbody>[TR]
[TD][/TD]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[TD]e[/TD]
[TD]i[/TD]
[TD]j[/TD]
[TD]k[/TD]
[TD]o[/TD]
[TD]p[/TD]
[TD]q[/TD]
[TD]r[/TD]
[TD]s[/TD]
[TD]t[/TD]
[TD]x[/TD]
[TD]y[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Project[/TD]
[TD][/TD]
[TD]T500 (Col:C-E)[/TD]
[TD][/TD]
[TD][/TD]
[TD]T502 (Col: i - K[/TD]
[TD][/TD]
[TD][/TD]
[TD]T504 (Col: O-Q)[/TD]
[TD][/TD]
[TD][/TD]
[TD]T505(Col: R-T)[/TD]
[TD][/TD]
[TD][/TD]
[TD]ETC[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Date[/TD]
[TD][/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD]Start[/TD]
[TD]Finish[/TD]
[TD]Total[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Make sure that the columns to hide have been entered in column E. Then enter an "N" in the appropriate row in column E and press the RETURN key.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub
    Dim splitCols As Variant
    If Target = "N" Then
        splitCols = Split(Target.Offset(0, 2), "-")
        With Sheets("Sheet2")
            .Columns.Hidden = False
            .Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = True
        End With
    End If
End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Make sure that the columns to hide have been entered in column E. Then enter an "N" in the appropriate row in column E and press the RETURN key.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub
    Dim splitCols As Variant
    If Target = "N" Then
        splitCols = Split(Target.Offset(0, 2), "-")
        With Sheets("Sheet2")
            .Columns.Hidden = False
            .Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = True
        End With
    End If
End Sub



Mumps, thanks for the quick response, I inserted code as specified in Microsoft Excel Objects/Sheet1 (Sheet1). in the Workshop panel, then saving as XLSM file. I received Run time error 9 Subscript out of range error message on the following code:

.Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = True

I have also realised that my original message should have been more clear. If I remove the "N" from Column C / against a project, I would also like the project to reappear (ie the relevant columns become unhidden - sheet 2) PS in your notes you refer to column E... I presume this was actually column c in sheet 1 where I have place "N" against the project.

Regards Alex
 
Upvote 0
I tested the macro using the data you posted and it worked properly. Based on how your data was posted, it looked like column E contained the columns to hide. I think that it would be easier to help and test possible solutions if I could work with your actual file which includes any macros you are currently using. Perhaps you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Include a detailed explanation of what you would like to do using a few examples from your data and referring to specific cells, rows, columns and worksheets. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Mumps,
Got it working!!!! Didnt update column references in column E as you specified on sheet 1. Sorry

At the moment it only hides the last "N" assigned to a project - ie it can only hides one at a time and previous project hidden with "N" reappear. I was hoping to hide/ unhide multiple projects/ column when I entered an N or removed it against a project?

Appreciate your support...
 
Upvote 0
Try this version. Enter "N" to hide and "Y" to unhide.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub
    Dim splitCols As Variant
    Select Case Target.Value
        Case "N"
            splitCols = Split(Target.Offset(0, 2), "-")
            With Sheets("Sheet2")
                .Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = True
            End With
        Case "Y"
            splitCols = Split(Target.Offset(0, 2), "-")
            With Sheets("Sheet2")
                .Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = False
            End With
    End Select
End Sub
 
Upvote 0
Try this version. Enter "N" to hide and "Y" to unhide.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub
    Dim splitCols As Variant
    Select Case Target.Value
        Case "N"
            splitCols = Split(Target.Offset(0, 2), "-")
            With Sheets("Sheet2")
                .Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = True
            End With
        Case "Y"
            splitCols = Split(Target.Offset(0, 2), "-")
            With Sheets("Sheet2")
                .Columns(splitCols(0) & ":" & splitCols(1)).EntireColumn.Hidden = False
            End With
    End Select
End Sub


Mumps, that is fantastic... cannot tell you how much I appreciate your help. It is after midnight here in South Australia on Saturday night - got to work tomorrow to get job done for Monday... just taken a load off my mind with your solution. Thanks again. Alex
 
Upvote 0
You are very welcome. Sleep well. :)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top