Solved

Hide Column if no data in Cell below

Posted on 2011-03-15
3
436 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now