?
Solved

Hide Column if no data in Cell below

Posted on 2011-03-15
3
Medium Priority
?
443 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 2000 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

Industry Leaders: 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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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