Solved

Hide Column if no data in Cell below

Posted on 2011-03-15
3
438 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
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:pari123
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

776 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