+ Reply to Thread
Results 1 to 33 of 33

Hide entire Table if cells empty

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Hide entire Table if cells empty

    Hi.
    I can find plenty of examples on how to hide rows of a table when empty using VBA but nothing to hide the entire table is the cells within that table are empty

    Can anyone give me a example on how to achieve this?

  2. #2
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2016
    Posts
    949

    Re: Hide entire Table if cells empty

    try

    Sub HideEmptyTables()
        Dim tbl As ListObject
        
        For Each tbl In ThisWorkbook.Worksheets("Sheet1").ListObjects
            
            If WorksheetFunction.CountA(tbl.Range.Offset(1)) = 0 Then
                tbl.Range.EntireRow.Hidden = True
            End If
        Next tbl
    End Sub

  3. #3
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Quote Originally Posted by k1dr0ck View Post
    try

    Sub HideEmptyTables()
        Dim tbl As ListObject
        
        For Each tbl In ThisWorkbook.Worksheets("Sheet1").ListObjects
            
            If WorksheetFunction.CountA(tbl.Range.Offset(1)) = 0 Then
                tbl.Range.EntireRow.Hidden = True
            End If
        Next tbl
    End Sub
    Not working for me. code doesn't do anything, no error

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    In this case, the table is not empty. Probably because of formulas that return an empty string.
    Try this
    Sub HideEmptyTables2()
      With ActiveSheet.ListObjects(1)
        
        If Len(Application.Concat(.Range.Offset(1))) = 0 Then
          .Range.EntireRow.Hidden = True
        End If
        
      End With
    End Sub
    Artik

  5. #5
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2016
    Posts
    949

    Re: Hide entire Table if cells empty

    curious why it does not do anything i tried it on a sample table where the rows are empty(not deleted) except the headers and it does hide the table

  6. #6
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Thanks for the replies.
    i made a new spreadsheet with a table and seems that the code works however doesn't work with my current worksheet

    Not sure if it has anything to do that the table in comes from a Power Query connection?

  7. #7
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2016
    Posts
    949

    Re: Hide entire Table if cells empty

    could you provide your sheet with desensitized data?

  8. #8
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    See attached
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2016
    Posts
    949

    Re: Hide entire Table if cells empty

    found a code here in the forum
    it filters the table showing only cells with values in column A so when all cells are blank in A then all cells are hidden although the headers are still visible
    is this what your desired output is?

    Sub test()
    Worksheets("Sheet1").ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:="<>"
    End Sub

  10. #10
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    The desired output would be to have every element of the table to be hidden including the headers and the total row

  11. #11
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2016
    Posts
    949

    Re: Hide entire Table if cells empty

    from your attached sheet the table has a single cell with a value so its not actually empty
    my first code was for a table that is empty except the headers
    maybe someone can provide what your desired output is with the attached sheet

  12. #12
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    The first table was a test to see if the formula works
    The second table has the query connection. This is the one that i would like the formula to work with

    Code works fine for the first table (when the cell data is deleted), but not for the second (when the data is deleted)

  13. #13
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2016
    Posts
    949

    Re: Hide entire Table if cells empty

    tried my code on post 2 on your attachment and it hides the second table

    i cleared the contents of all the cells of the second table not including the headers then ran the code

  14. #14
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Finally diagnosed the problem

    Seems that Artik's code works only if there is no totals row for the table
    Is there a way to incorporate this into the code?

  15. #15
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    Try this:
    Sub HideEmptyTables3()
    
        With ActiveSheet.ListObjects(1)
            If .ListRows.Count > 0 Then
                If Len(Application.Concat(.DataBodyRange.Value)) = 0 Then
                    .Range.EntireRow.Hidden = True
                End If
            Else
                .Range.EntireRow.Hidden = True
            End If
        End With
        
    End Sub
    Now you see how important it is to attach an adequate example to your question (because the one attached does not reflect the layout of your actual data). Three days of foam-balling what could have been dealt with in one day.

    Artik

  16. #16
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    I do apologise
    I made the attached without the totals row and didn't notice.
    Ill make more of a effort next time

  17. #17
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    If i was to do this to multiple tables, would there be a more efficient way of doing this or is repeating the code with a different ListObject Number the best/only way?

  18. #18
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    Yes, there are fans of Ctrl+C and Ctrl+V shortcuts in the world, but you can also think of a loop, such as:
    Sub HideEmptyTables4()
        Dim i           As Long
    
        With ActiveSheet
            For i = 1 To .ListObjects.Count
                With .ListObjects(i)
                    If .ListRows.Count > 0 Then
                        If Len(Application.Concat(.DataBodyRange.Value)) = 0 Then
                            .Range.EntireRow.Hidden = True
                        End If
                    Else
                        .Range.EntireRow.Hidden = True
                    End If
                End With
            Next i
        End With
    End Sub
    or
    Sub HideEmptyTables5()
        Dim LO          As ListObject
    
        For Each LO In ActiveSheet.ListObjects
            With LO
                If .ListRows.Count > 0 Then
                    If Len(Application.Concat(.DataBodyRange.Value)) = 0 Then
                        .Range.EntireRow.Hidden = True
                    End If
                Else
                    .Range.EntireRow.Hidden = True
                End If
            End With
        Next LO
    
    End Sub
    Artik

  19. #19
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Is there also a way to hide the first row ABOVE the table if the table is hidden?

  20. #20
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Perfect, works well

    Thank you for your help Artik.
    Ill close the thread

  21. #21
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    No security. It seems to be enough to change line
    .Range.EntireRow.Hidden = True
    to
    .Range.Offset(-1).Resize(.Range.Rows.Count + 1).EntireRow.Hidden = True
    Artik

  22. #22
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    This works great. But i assumed it would unhide the row Above when the table is not empty?

  23. #23
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    I guess I don't understand the problem.

    Artik

  24. #24
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    no you were correct

    I made an assumption that the row above would hide when there was data in the table & unhide when there was no data in the table

    Is this possible?

  25. #25
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    Maybe
    Sub HideEmptyTables6()
        Dim i           As Long
        Dim blnCondition As Boolean
    
        With Me
    
            For i = 1 To .ListObjects.Count
    
                With .ListObjects(i)
                    If .ListRows.Count > 0 Then
    
                        blnCondition = (Len(Application.Concat(.DataBodyRange.Value)) > 0)
    
                        .HeaderRowRange.Offset(-1).EntireRow.Hidden = blnCondition
                        .Range.EntireRow.Hidden = Not blnCondition
    
                    Else
                        .HeaderRowRange.Offset(-1).EntireRow.Hidden = False
                        .Range.EntireRow.Hidden = True
                    End If
                End With
    
            Next i
    
        End With
    End Sub
    Artik

  26. #26
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Thanks for your response
    However it shows a "Compile error" Invalid use of Me keyword

  27. #27
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,293

    Re: Hide entire Table if cells empty

    Replace Me with ActiveSheet.

    Artik

  28. #28
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Although mostly working

    The code doesn't unhide the row above the tables that are visible

    It hides all the every row above every table instead whether the table is visible or not

  29. #29
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Hide entire Table if cells empty

    Quote Originally Posted by neek0la View Post
    Although mostly working

    The code doesn't unhide the row above the tables that are visible

    It hides all the every row above every table instead whether the table is visible or not
    Hi. It seems to me that the following VBA code covers all angles of the problem:


    Sub Macro8()
    Dim tb As ListObject, Rng As Range, cHidden As Boolean
    '-------------->
    For Each tb In ActiveSheet.ListObjects
      If tb.HeaderRowRange.Row > 1 Then
        Set Rng = Union(tb.HeaderRowRange.Offset(-1), tb.Range)
      Else
        Set Rng = tb.Range
      End If
    '-------------->
      Select Case True
        Case tb.ListRows.Count = 0: cHidden = True
        Case Else
          If Application.CountBlank(tb.DataBodyRange) = tb.DataBodyRange.Count Then cHidden = True
      End Select
      Rng.EntireRow.Hidden = cHidden
    Next
    '-------------->
    End Sub
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  30. #30
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    Quote Originally Posted by beyond Excel View Post
    Hi. It seems to me that the following VBA code covers all angles of the problem:


    Sub Macro8()
    Dim tb As ListObject, Rng As Range, cHidden As Boolean
    '-------------->
    For Each tb In ActiveSheet.ListObjects
      If tb.HeaderRowRange.Row > 1 Then
        Set Rng = Union(tb.HeaderRowRange.Offset(-1), tb.Range)
      Else
        Set Rng = tb.Range
      End If
    '-------------->
      Select Case True
        Case tb.ListRows.Count = 0: cHidden = True
        Case Else
          If Application.CountBlank(tb.DataBodyRange) = tb.DataBodyRange.Count Then cHidden = True
      End Select
      Rng.EntireRow.Hidden = cHidden
    Next
    '-------------->
    End Sub
    This just hides all the tables from what i can see

    Artik is very close. Just need to figure how to unhide the Row above tables that are visible

  31. #31
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Hide entire Table if cells empty

    And it also shows you the tables as soon as they start to have data: Is it verified?

  32. #32
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    Re: Hide entire Table if cells empty

    As soon as i run the code everything is hidden. Including tables with Data in them

  33. #33
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Hide entire Table if cells empty

    Quote Originally Posted by neek0la View Post
    As soon as i run the code everything is hidden. Including tables with Data in them
    Please: upload your sample workbook with the implemented code on the forum to see the same thing you are seeing...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 08-30-2019, 09:56 AM
  2. [SOLVED] Hide empty cells
    By PNick in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-15-2018, 12:16 PM
  3. [SOLVED] Hide Entire Row Based On Values of Cells in that Row
    By Matthew55 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-21-2017, 12:48 PM
  4. Hide entire row only when all values in noncalculated cells are zero
    By nyt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2015, 11:02 AM
  5. [SOLVED] Hide / Unhide rows in a table that the first column cells are empty
    By How How in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-09-2014, 05:46 AM
  6. Delete entire column if all cells in a certain range are empty
    By Andula in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-26-2011, 06:33 AM
  7. macro to delete entire rows if certain cells are empty
    By wrightie in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-07-2009, 05:46 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1