+ Reply to Thread
Results 1 to 5 of 5

hide columns if they are empty populated from userform

Hybrid View

KK1234 hide columns if they are... 04-07-2014, 06:56 AM
MarvinP Re: hide columns if they are... 04-07-2014, 07:19 AM
MarvinP Re: hide columns if they are... 04-07-2014, 07:32 AM
KK1234 Re: hide columns if they are... 04-07-2014, 08:08 AM
KK1234 Re: hide columns if they are... 04-07-2014, 08:10 AM
  1. #1
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    hide columns if they are empty populated from userform

    Hi all,
    Following code works fine. It will hide column if row 2 is empty ( But only for data in row 2 of the respective columns)
    How do I get it it work for the entire columns?
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Range("J2").Value = "" Then
            Columns("J").EntireColumn.Hidden = True
        Else
            Columns("J").EntireColumn.Hidden = False
        End If
    If Range("H2").Value = "" Then
            Columns("H").EntireColumn.Hidden = True
        Else
            Columns("H").EntireColumn.Hidden = False
        End If
    If Range("I2").Value = "" Then
            Columns("I").EntireColumn.Hidden = True
        Else
            Columns("I").EntireColumn.Hidden = False
        End If
    If Range("K2").Value = "" Then
            Columns("K").EntireColumn.Hidden = True
        Else
            Columns("K").EntireColumn.Hidden = False
        End If
    
    If Range("L2").Value = "" Then
            Columns("L").EntireColumn.Hidden = True
        Else
            Columns("L").EntireColumn.Hidden = False
        End If
    
    If Range("M2").Value = "" Then
            Columns("M").EntireColumn.Hidden = True
        Else
            Columns("M").EntireColumn.Hidden = False
        End If
    If Range("F2").Value = "" Then
            Columns("F").EntireColumn.Hidden = True
        Else
            Columns("F").EntireColumn.Hidden = False
        End If
    If Range("G2").Value = "" Then
            Columns("G").EntireColumn.Hidden = True
        Else
            Columns("G").EntireColumn.Hidden = False
        End If
    If Range("N2").Value = "" Then
            Columns("N").EntireColumn.Hidden = True
        Else
            Columns("N").EntireColumn.Hidden = False
        End If
    
    
    End Sub
    Test file is attached
    Need some help please
    Kind regards
    Attached Files Attached Files

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,252

    Re: hide columns if they are empty populated from userform

    Hi KK,
    Try this code instead of yours.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim ColCtr As Double
        For ColCtr = 6 To 14
            If Cells(Rows.Count, ColCtr).End(xlUp).Row = 1 Then
                Columns(ColCtr).EntireColumn.Hidden = True
            Else
                Columns(ColCtr).EntireColumn.Hidden = False
            End If
        Next ColCtr
    End Sub
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,252

    Re: hide columns if they are empty populated from userform

    If you don't want to count from 6 to 14 and use column letters then this will work also.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim ColCtr As Double
        For ColCtr = Cells(, "F").Column To Cells(, "N").Column
            If Cells(Rows.Count, ColCtr).End(xlUp).Row = 1 Then
                Columns(ColCtr).EntireColumn.Hidden = True
            Else
                Columns(ColCtr).EntireColumn.Hidden = False
            End If
        Next ColCtr
    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: hide columns if they are empty populated from userform

    Hi MarvinP,
    Great work.
    I have understood your code and it works fine.
    Really appriciated your help.
    Many thanks
    Kind regards

  5. #5
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: hide columns if they are empty populated from userform

    Hi again,
    I have two more questions, however as per forum rule they will have to be in another Thread.
    See you soon
    Kind regards

+ 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 empty columns Toggle
    By gherzberg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2012, 04:15 PM
  2. [SOLVED] Hide empty rows of shown columns
    By wilco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-17-2012, 05:42 PM
  3. VBA code to hide empty columns.
    By ms_ex_novice in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-29-2011, 10:43 AM
  4. [SOLVED] Un hide non empty columns
    By hamad.fatima@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2006, 03:28 AM
  5. Un hide non empty columns
    By hamad.fatima@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-06-2006, 04:39 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