Hutchington
New Member
- Joined
- Jan 23, 2014
- Messages
- 4
I am hoping that someone can help me. I have the below code that loops through every worksheet in my workbook to find instances of names and makes all of these worksheets visible. In the Range "B2:B300" is a list of all the tabs worksheets within the workbook. Each tab can dynamically be made visible by inputting a "Y" in the cells "C2:C300", and likewise hidden by inputting an "N".
What I want to do is make the above dynamic too so I am not relying on a macro. I have converted the above into a Case Statement, but I cannot get it to work. The idea is, if you put a "Y" in a field within the range F15:F100 it will make visible all worksheets with that have the name in the offset cell. If you put an "N" in all the cells within the range C2:C300 with that dynamic code, thereby closing all of the tabs. It would then run the code in WS_Change() to see if there are any other cells with a "Y" in them and thereby reopen all the tabs with names still selected. That's the plan, but I can't seem to get it to work. Would someone mind having a look at the below and point out where I am going wrong, please.
VBA Code:
Sub WS_Change()
Dim ws As Worksheet
Dim wkb As Workbook
Dim cel As Range
Dim cel2 As Range
Dim TN As Range
Dim TN2 As Range
Dim CDS As Object
Set wkb = ActiveWorkbook
Set ws = wkb.Worksheets(1)
Set CDS = Sheets("Control")
For Each cel In Range("F15:F100")
If UCase(cel.Value = "Y") Then
For Each ws In Sheets 'This statement starts the loop
For Each cel2 In ws.Range("A2:A100")
If cel.Offset(0, -1).Value = cel2.Value Then
ws.Visible = True
Exit For
End If
Next cel2
Next ws
For Each ws In Worksheets
If ws.Visible = xlSheetVisible Then
For Each TN In CDS.Range("B2:B300")
If ws.Name = TN.Value Then
TN.Offset(0, 1) = "Y"
End If
Next TN
End If
Next ws
End If
Next cel
Call CDS.Activate
End Sub�
What I want to do is make the above dynamic too so I am not relying on a macro. I have converted the above into a Case Statement, but I cannot get it to work. The idea is, if you put a "Y" in a field within the range F15:F100 it will make visible all worksheets with that have the name in the offset cell. If you put an "N" in all the cells within the range C2:C300 with that dynamic code, thereby closing all of the tabs. It would then run the code in WS_Change() to see if there are any other cells with a "Y" in them and thereby reopen all the tabs with names still selected. That's the plan, but I can't seem to get it to work. Would someone mind having a look at the below and point out where I am going wrong, please.
VBA Code:
Sub WrkS_Change(ByVal Target As Range)
'Sub WS_Change()
Dim ws As Worksheet
Dim wkb As Workbook
Dim cel As Range
Dim cel2 As Range
Dim TN As Range
Dim TN2 As Range
Dim CDS As Object
Set wkb = ActiveWorkbook
Set ws = wkb.Worksheets(1)
Set CDS = Sheets("Control")
'On Error GoTo EH
If Application.Intersect(Target, CDS.Range(Cells(15, 6), Cells(100, 6))) Is Nothing Then Exit Sub
'Application.EnableEvents = False
Select Case UCase(Target.Value)
Case "Y"
For Each ws In Sheets
For Each cel2 In ws.Range("A2:A100")
If Cells(Target.Row, Target.Column - 1).Value = cel2.Value Then
ws.Visible = True
Exit For
End If
Next cel2
Next ws
For Each ws In Worksheets
If ws.Visible = xlSheetVisible Then
For Each TN In CDS.Range("B2:B300")
If ws.Name = TN.Value Then
TN.Offset(0, 1) = "Y"
End If
Next TN
End If
Next ws
Case "N"
For Each TN2 In CDS.Range("C2:C239")
TN2 = "N"
Next TN2
For Each cel In Range("F15:F100")
If UCase(cel.Value = "Y") Then
For Each ws In Sheets 'This statement starts the loop
For Each cel2 In ws.Range("A2:A100")
If cel.Offset(0, -1).Value = cel2.Value Then
ws.Visible = True
Exit For
End If
Next cel2
Next ws
For Each ws In Worksheets
If ws.Visible = xlSheetVisible Then
For Each TN In CDS.Range("B2:B300")
If ws.Name = TN.Value Then
TN.Offset(0, 1) = "Y"
End If
Next TN
End If
Next ws
End If
Next cel
End Select
'EH:
'Application.EnableEvents = True
End Sub�