+ Reply to Thread
Results 1 to 10 of 10

VBA code to hide empty columns.

Hybrid View

ms_ex_novice VBA code to hide empty... 12-28-2011, 10:54 AM
Firefly2012 Re: VBA code to hide empty... 12-28-2011, 11:10 AM
ms_ex_novice Re: VBA code to hide empty... 12-28-2011, 11:26 AM
Firefly2012 Re: VBA code to hide empty... 12-28-2011, 11:40 AM
ms_ex_novice Re: VBA code to hide empty... 12-28-2011, 12:11 PM
ms_ex_novice Re: VBA code to hide empty... 12-28-2011, 04:01 PM
Firefly2012 Re: VBA code to hide empty... 12-29-2011, 05:59 AM
ms_ex_novice Re: VBA code to hide empty... 12-29-2011, 10:14 AM
Firefly2012 Re: VBA code to hide empty... 12-29-2011, 10:32 AM
ms_ex_novice Re: VBA code to hide empty... 12-29-2011, 10:43 AM
  1. #1
    Registered User
    Join Date
    11-28-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    11

    VBA code to hide empty columns.

    Hi.

    I need to create VBA code to hide empty columns. Problem is when column considered empty it always has header row(s). I guess code should have defined Range of rows and columns such as A2:AZ50. In this case row is header. Thanks for your help.

  2. #2
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: VBA code to hide empty columns.

    Hi
    If you have only the one header row then you could account for this eg with:

    Sub Hide_Columns()
    Dim col As Range
    
    For Each col In ActiveSheet.UsedRange.Columns
        If Application.CountA(col) <= 1 Then col.Hidden = True
    Next
    
    End Sub

  3. #3
    Registered User
    Join Date
    11-28-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA code to hide empty columns.

    Hi,
    Somehow, it does not work for on row header.
    Thanks.

  4. #4
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: VBA code to hide empty columns.

    The code as posted will hide any columns which contains either only a single item of data or no items of data (so it assumes that each column has a header value which would therefore count as a single item of data). If this is not working as you would want it to, then you need to explain what is happening that you don't want to happen and also what is not happening that you want to happen.

    Perhaps attach an example of your worksheet and explain which columns should be hidden and why?

  5. #5
    Registered User
    Join Date
    11-28-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA code to hide empty columns.

    Hi,
    You are right. Somehow it did not work with excel worksheet exported from Access. When I created brand new worksheet it works fine. Sorry and thank you so much for your help.

  6. #6
    Registered User
    Join Date
    11-28-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA code to hide empty columns.

    Hi,
    One thing still is confusing me. Excel workbook that I want to apply VBA macro is exported from Access. Column that looks like blank is not hidden by this macro. But when I highlight the same column, delete values (even it looks like no values and column is blank) and run macro again, it works. Can you please help understand what walues could be there and why they exported from Access. Thanks

  7. #7
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: VBA code to hide empty columns.

    Do you have any spaces in the column that could otherwise look like blank cells?

  8. #8
    Registered User
    Join Date
    11-28-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA code to hide empty columns.

    No spaces.

  9. #9
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: VBA code to hide empty columns.

    Humour me and see if this code works any better:

    Sub Hide_Columns()
    Dim col As Range, rFound As Range
    
    For Each col In ActiveSheet.UsedRange.Columns
        Set rFound = col.Find(What:="?*",SearchDirection:=xlPrevious)
        If Not rFound Is Nothing Then
           If rFound.Row = 1 Then col.Hidden = True
        Else
           col.Hidden = True
        End If
        Set rFound = Nothing
    Next
    
    End Sub

  10. #10
    Registered User
    Join Date
    11-28-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA code to hide empty columns.

    Hi,
    I was going to upload my worksheet but this macro works absolutely perfect. Thank you so much.

+ 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