+ Reply to Thread
Results 1 to 10 of 10

Hide Columns IF Row 2 onwards is Blank

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    349

    Hide Columns IF Row 2 onwards is Blank

    The following code works..sorta. I can see that it's hiding the columns I want/are blank but then it immediately unhides them. What I want is when I press the button once, it hides them and then press it again, unhides them. Any idea where I went wrong?

    Sub ToggleButton1_Click()
    If ToggleButton1.Value = True Then
    
    Dim i As Long
        
    For i = 2 To 12
        If Sheets("Comments").Columns(i).Rows(1).End(xlDown).Value = "" Then
        Columns(i).EntireColumn.Hidden = True
    End If
        Next i
    For i = 2 To 12
        Columns(i).EntireColumn.Hidden = False
        Next i
    
    If ToggleButton1.Caption = "Hide Blank Columns" Then
            ToggleButton1.Caption = "Show All"
        Else
            ToggleButton1.Caption = "Hide Blank Columns"
        End If
        End If
    End Sub

  2. #2
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: Hide Columns IF Row 2 onwards is Blank

    Can you upload the workbook with this code for better understanding.
    Lokesh Kumar
    Stay Hungry.. Stay Foolish..
    _________________________________________________________
    Please Click STAR to Add Reputation if my/someone's answer helped!

  3. #3
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    349

    Re: Hide Columns IF Row 2 onwards is Blank

    Here you go! You can ignore the blue buttons (actually just shapes). They don't have code attached anymore. The grey toggle button is my issue, thanks!
    Attached Files Attached Files

  4. #4
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: Hide Columns IF Row 2 onwards is Blank

    Hi,

    Can you check now it is working correctly?......Book1.xlsm

    If your question is resolved, mark it SOLVED using the thread tools. Click on the star if you think some-1 helped you.

  5. #5
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    349

    Re: Hide Columns IF Row 2 onwards is Blank

    not quite... for instance column C, E, I and K are blank whereas the others have a 1 in them. I only want the blank columns hidden.

  6. #6
    Forum Contributor LokeshKumar's Avatar
    Join Date
    03-31-2015
    Location
    India
    MS-Off Ver
    All, mostly 2010 now..
    Posts
    471

    Re: Hide Columns IF Row 2 onwards is Blank

    Quote Originally Posted by Wheelie686 View Post
    not quite... for instance column C, E, I and K are blank whereas the others have a 1 in them. I only want the blank columns hidden.
    Hey sorry for bad explanation from my end. I was expecting you to be a familiar with VBA so I just removed the unnecessary part.
    You can easily put the if else condition to check the blank.
    If still face any issue pls let us know...........

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Hide Columns IF Row 2 onwards is Blank

    Maybe?

    Sub Wheelie686()
    
    Dim i As Long
    If ToggleButton1.Value = True Then
        ToggleButton1.Value = False
    Else
        ToggleButton1.Value = True
    End If
    If ToggleButton1.Caption = "Show All" Then
            For i = 12 To 2 Step -1
                Columns(i).Hidden = False
            Next i
    ElseIf ToggleButton1.Caption = "Hide Blank Columns" Then
            For i = 12 To 2 Step -1
                If Cells(2, i) = "" Then
                    Columns(i).Hidden = True
                End If
            Next i
    End If
    
    
    End Sub

  8. #8
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    349

    Re: Hide Columns IF Row 2 onwards is Blank

    LokeshKumar, maybe it's me trying to be at work and mess with Excel at the same time but I just can't seem to get the coding right. I have some knowledge of VBA but I'm self taught through (mainly) this site and using the macro recorder. This is what I have so far. It hides the appropriate columns now but doesn't unhide anything.

    Option Explicit
    
    Sub ToggleButton1_Click()
        
        Dim i As Long
        
            If ToggleButton1.Value = True Then
    
            For i = 2 To 12
    
            If Sheets("Comments").Columns(i).Rows(1).End(xlDown).Value = "" Then
            Columns(i).EntireColumn.Hidden = True
            End If
                Next i
            
            If ToggleButton1.Value = False Then
    
                For i = 2 To 12
                
            Columns(i).EntireColumn.Hidden = False
                Next i
            End If
            
            If ToggleButton1.Caption = "Hide Blank Columns" Then
            ToggleButton1.Caption = "Show All"
                Else
                    ToggleButton1.Caption = "Hide Blank Columns"
            End If
            End If
    End Sub
    Last edited by Wheelie686; 05-01-2015 at 04:56 PM.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Hide Columns IF Row 2 onwards is Blank

    Maybe:

    If ToggleButton1.Value = False Then Columns.Hidden = False
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  10. #10
    Forum Contributor
    Join Date
    05-27-2012
    Location
    New Brunswick, Canada
    MS-Off Ver
    Excel 2016
    Posts
    349

    Re: Hide Columns IF Row 2 onwards is Blank

    omg I forgot Else...literally forgot the word "Else". Thanks everyone... I guess I just needed to focus on what I'm doing.

    Sub ToggleButton1_Click()
    If ToggleButton1.Value = True Then
    
    Dim i As Long
        
    For i = 2 To 12
        If Sheets("Comments").Columns(i).Rows(1).End(xlDown).Value = "" Then
        Columns(i).EntireColumn.Hidden = True
    End If
        Next i
    Else
    For i = 2 To 12
        Columns(i).EntireColumn.Hidden = False
        Next i
    
    If ToggleButton1.Caption = "Hide Blank Columns" Then
            ToggleButton1.Caption = "Show All"
        Else
            ToggleButton1.Caption = "Hide Blank Columns"
        End If
        End If
    End Sub
    Now to figure out why the button name doesn't work right...
    Last edited by Wheelie686; 05-01-2015 at 06:25 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Hide columns if empty from Row 3 onwards vba
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2014, 03:41 AM
  2. Hide non adjacent columns if all cell value is blank
    By RT_516 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-09-2014, 06:17 PM
  3. Hide columns if all cells are blank or £0.00
    By Atticgirl in forum Excel General
    Replies: 9
    Last Post: 07-21-2011, 08:44 AM
  4. Hide blank columns via loop
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2011, 10:17 PM

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