Solved

Hide Column if no data in Cell below

Posted on 2011-03-15
3
442 Views
Last Modified: 2012-12-10
I have a excel spreadsheet and the spreadsheet has Headings in row 1.  
What I need, on open if there is a Heading in row 1 but the cells below have no data then to hide that column.

Any help would greatly be appreciated.
0
Comment
Question by:ca1358
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 35142181
Try this:
Sub x()

Dim c As Long

With ActiveSheet.UsedRange
    For c = 1 To .Columns.Count
        If Cells(1, c) <> "" And WorksheetFunction.CountA(.Columns(c)) = 1 Then
            .Columns(c).Hidden = True
        End If
    Next c
End With

End Sub

Open in new window

0
 
LVL 20

Expert Comment

by:Ardhendu Sarangi
ID: 35142197
Hi,
Can you try this code -

Sub HideME()
    Set ws = ActiveSheet ' SELECT CURRENT SHEET
    Set Rng = ws.Range("A1:ZZ65536") ' DEFINE RANGE
    For Each Col In Rng.Columns ' START SEARCHING THE RANGE FOR EMPTY COLUMNS
        'MsgBox (Mid(Col.Address, 2, 1))
        'MsgBox (Cells(65536, Mid(Col.Address, 2, 1)).End(xlUp).Row)
        If Cells(65536, Mid(Col.Address, 2, 1)).End(xlUp).Row = 1 Then
            Rng.Columns(Col.Column).EntireColumn.Hidden = True ' Hide COlumn if this is empty
        End If
    Next Col
End Sub

Open in new window


Thanks,
Ardhendu
0
 

Author Closing Comment

by:ca1358
ID: 35211949
Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question