+ Reply to Thread
Results 1 to 6 of 6

How to hide all empty columns without including the column header

Hybrid View

  1. #1
    Registered User
    Join Date
    11-09-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    3

    How to hide all empty columns without including the column header

    I have a spreadsheet that will have column headers. But some of the columns will be empty. I need to determine which columns are empty, minus the column headers, and hide them all. I've tried several things but I'm sure I'm missing something.

    Sub HideEmptyCols()
        ' Deletes all empty columns on the active worksheet
        Dim iCol As Integer
        Dim wsSheet As Worksheet
     
        For Each wsSheet In Worksheets
            wsSheet.Select
            With wsSheet.UsedRange
                For iCol = .Column + .Columns.Count - 1 To 1 Step -1
                    If IsEmpty(Cells(65536, iCol)) And IsEmpty(Cells(1, iCol)) Then
                        If Cells(65536, iCol).End(xlUp).Row = 1 Then Columns(iCol).Hidden = True
                    MsgBox (iCol)
                    End If
                    MsgBox (iCol)
                Next iCol
            End With
        Next wsSheet
    End Sub
    Last edited by mkingsberry; 11-09-2010 at 12:06 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to hide all empty columns without including the column header

    Hi mkingsberry. Welcome to the forum.

    Be sure to read through the Forum Rules so you can use and follow them effectively. For instance, you'll need to EDIT that post above and put code tags around that code you used. (Like shown here).

    Option Explicit
    
    Sub HideEmptyColumns()
    Dim ws As Worksheet
    Dim LastCol As Long
    Dim Col As Long
    
    For Each ws In Worksheets
        LastCol = ws.Cells.Find("*", ws.Cells(ws.Rows.Count, ws.Columns.Count), _
            SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
        For Col = 1 To LastCol
            If Application.WorksheetFunction.CountA(ws.Columns(Col)) < 2 Then _
                ws.Columns(Col).Hidden = True
        Next Col
    
    Next ws
    
    End Sub

    That should work. Now be sure to fix that post above. Thanks.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to hide all empty columns without including the column header

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    11-09-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: How to hide all empty columns without including the column header

    Sorry about that. I've formatted the code properly.

    Michael

  5. #5
    Registered User
    Join Date
    11-09-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: How to hide all empty columns without including the column header

    Thanks Jerry. It works great!

    Michael

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to hide all empty columns without including the column header

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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