Hey All,
I have the following code which does what I want.: Hide columns dependent on the input in a cell. I want this to work in all the worksheets. I have only just started playing around with VBA and from reading a few posts this could be done with a loop function but I'm not sure my code is in the correct form...
Any advice on how to get this to work across all the sheets would be greatly appreciatedPrivate Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Count = 1 And Target.Address = "$G$26" Then If LCase(Target.Value) = "5" Then Columns("O:AH").EntireColumn.Hidden = True ElseIf LCase(Target.Value) = "10" Then Columns("O:AH").EntireColumn.Hidden = False Columns("T:AH").EntireColumn.Hidden = True ElseIf LCase(Target.Value) = "15" Then Columns("T:AH").EntireColumn.Hidden = False Columns("Y:AH").EntireColumn.Hidden = True ElseIf LCase(Target.Value) = "20" Then Columns("Z:AG").EntireColumn.Hidden = False Columns("AD:AH").EntireColumn.Hidden = True ElseIf LCase(Target.Value) = "25" Then Columns("AD:AH").EntireColumn.Hidden = False Else Columns("B:M").EntireColumn.Hidden = False End If End If End Sub
Hi acj06
Place the code in ThisWorkbook module as a Workbook_SheetChange event.
John
If you have issues with Code I've provided, I appreciate your feedback.
In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.
Hi acj06 and welcome to the forum. If you want that particular procedure to work on other sheets, simply copy it and paste it into other sheet modules.
Please leave a message after the beep!
Thanks for the advice, but I want it to run it off the one cell in one sheet, so i enter a multiple of 5 in $g$26 in worksheet("Inputs") and the columns dissapear dependent on that input...Any advice?
Hi acj06
Probably something like thisOption Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Worksheet If Target.Cells.Count = 1 And Target.Address = "$G$26" Then If LCase(Target.Value) = "5" Then For Each ws In ActiveWorkbook.Worksheets ws.Columns("O:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "10" Then For Each ws In ActiveWorkbook.Worksheets ws.Columns("O:AH").EntireColumn.Hidden = False ws.Columns("T:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "15" Then For Each ws In ActiveWorkbook.Worksheets ws.Columns("T:AH").EntireColumn.Hidden = False ws.Columns("Y:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "20" Then For Each ws In ActiveWorkbook.Worksheets ws.Columns("Z:AG").EntireColumn.Hidden = False ws.Columns("AD:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "25" Then For Each ws In ActiveWorkbook.Worksheets ws.Columns("AD:AH").EntireColumn.Hidden = False Next ws Else For Each ws In ActiveWorkbook.Worksheets ws.Columns("B:M").EntireColumn.Hidden = False Next ws End If End If End Sub
John
If you have issues with Code I've provided, I appreciate your feedback.
In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.
John,
The code works excellently! Thank you. I was wondering if i wanted to specify which worksheets to hide columns in, how would I tell excel which sheets to hide the columns in?
Thanks Again
Alex
Hi acj06
Replace these lines of codewith thisFor Each ws In ActiveWorkbook.WorksheetsFor Each ws In Array("Sheet1", "Sheet3", "Sheet5", "Sheet7")
John
If you have issues with Code I've provided, I appreciate your feedback.
In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.
John,
Everything was working a treat, I continued building my model...I went to change the &G&26 input and now i get the following error:
Run-time error '1004':
John,
Thanks for your help it is invaluable. The code was working perfectly, so i continued working in excel. Only now to find when I try and change the value in cell &G&26, i ge the following runtime error:
Run time error '1004'
unable to set the Hidden property of the range class
Do you have any suggestions on how to get around this problem. Thanks for the advice!
Alex
Hi Alex
You probably should have gotten "Object Required". Try this revised codeOption Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Variant If Target.Cells.Count = 1 And Target.Address = "$G$26" Then If LCase(Target.Value) = "5" Then For Each ws In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5") Sheets(ws).Columns("O:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "10" Then For Each ws In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5") Sheets(ws).Columns("O:AH").EntireColumn.Hidden = False Sheets(ws).Columns("T:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "15" Then For Each ws In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5") Sheets(ws).Columns("T:AH").EntireColumn.Hidden = False Sheets(ws).Columns("Y:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "20" Then For Each ws In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5") Sheets(ws).Columns("Z:AG").EntireColumn.Hidden = False Sheets(ws).Columns("AD:AH").EntireColumn.Hidden = True Next ws ElseIf LCase(Target.Value) = "25" Then For Each ws In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5") Sheets(ws).Columns("AD:AH").EntireColumn.Hidden = False Next ws Else For Each ws In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5") Sheets(ws).Columns("B:M").EntireColumn.Hidden = False Next ws End If End If End Sub
John
If you have issues with Code I've provided, I appreciate your feedback.
In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.
John,
Thanks that works brilliantly!
Alex
Hi Alex
You're welcome...glad I could be of help. If that satisfies your need, I'd appreciate it if you'll please mark your thread as "Solved".
To mark your thread solved do the following:
- Go to your first post on the thread
- Click edit
- Click Advance
- Just below the word "Title:" you will see a dropdown with the word No prefix.
- Change to Solved
- Click Save
John
If you have issues with Code I've provided, I appreciate your feedback.
In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks