Hide / Unhide tables based on a list chosen

Capps

New Member
Joined
Aug 14, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi all,

I’ve got a sheet with 4 tables on it.

I have a cell in B2 with data validation set to a list on another sheet.

I can currently hide or show the tables based on either columns or rows. As one table is shown the others are hidden.

What I’m wandering is if it is possible to hide or unhide each table that allows the tables to be updated and expanded without editing the number of rows or the columns in the code every so often.

Matt
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi & welcome to MrExcel.
What code do you currently use?
 
Upvote 0
Here’s what I did for hiding and showing rows

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("B1"), Range(Target.Address)) Is Nothing Then
        Select Case Target.Value
            Case Is = "PROJECT INFO":
                                    ListObjects("PROJECT_INFO").Range.Select
                                    Rows("22:244").EntireRow.Hidden = True
                                    Rows("6:29").EntireRow.Hidden = False
            Case Is = "PRODUCT INFO":
                                    Rows("6:27").EntireRow.Hidden = True
                                    Rows("28:34").EntireRow.Hidden = False
                                    Rows("35:244").EntireRow.Hidden = True
            Case Is = "SONG INFO":
                                    Rows("6:39").EntireRow.Hidden = True
                                    Rows("40:80").EntireRow.Hidden = False
                                    Rows("81:244").EntireRow.Hidden = True
            Case Is = "SONG WRITER INFO":
                                    Rows("6:121").EntireRow.Hidden = True
                                    Rows("122:138").EntireRow.Hidden = False
                                    Rows("139:244").EntireRow.Hidden = True
        End Select
End If
End Sub

And here is what I did for hiding the columns

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("B1"), Range(Target.Address)) Is Nothing Then
        Select Case Target.Value
        Case Is = "PROJECT INFO": Columns("E:EG").EntireColumn.Hidden = True
                                  Columns("C:D").EntireColumn.Hidden = False
        Case Is = "PRODUCT INFO": Columns("K:EG").EntireColumn.Hidden = True
                                  Columns("F:J").EntireColumn.Hidden = False
                                  Columns("C:E").EntireColumn.Hidden = True
        Case Is = "SONG WRITER INFO":
                                  Columns("C:K").EntireColumn.Hidden = True
                                  Columns("L:AL").EntireColumn.Hidden = False
                                  Columns("AM:EG").EntireColumn.Hidden = True
        Case Is = "SONG INFO"
                                  Columns("C:AL").EntireColumn.Hidden = True
                                  Columns("AN:BU").EntireColumn.Hidden = False
                                  Columns("BU:EG").EntireColumn.Hidden = True
                        
        End Select
End If
End Sub
 
Last edited by a moderator:
Upvote 0
Is your data actually in structured tables?
Also you can only have one change event per sheet, so is this working on two different sheets?
 
Upvote 0
The data is structured in tables each to be filled in as the project builds.
 
Upvote 0
In that case what are the table names?
Also can you answer the other part of my question?
 
Upvote 0
The tables names are:

“Project Info”
“Product Info”
“Song Info”
“Song Writer Info”

These are all on one sheet
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim i As Long
   
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("B1")) Is Nothing Then
      With Me.ListObjects
         For i = 1 To .Count
            If Replace(.Item(i).Name, "_", " ") = Target.Value Then
               .Item(i).HeaderRowRange.Columns.Hidden = False
            Else
               .Item(i).HeaderRowRange.Columns.Hidden = True
            End If
         Next i
      End With
   End If
End Sub
 
Upvote 0
That worked brilliantly. Thank you sooo much.
Matt
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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