Results 1 to 3 of 3

Thread: Hide/Unhide Columns with Listbox

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Hide/Unhide Columns with Listbox

    In the Listbox, for example i select the months of January, March and December, I want only show me the columns with the name of the selected months in the row 10 and the other columns are hidden.
    Can you help me?
    Greetings.
    Attached Files Attached Files

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Hi obed_cruz

    Welcome to ExcelFox

    Use this in the userform
    Code:
    Private Sub MOSTAR_MES_Click()
    
    Dim rngCell As Range
    Dim rngUnion As Range
    Dim lngListLoop As Long
    Dim strMostar As String
    
    For lngListLoop = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lngListLoop) Then
            strMostar = strMostar & "|" & ListBox1.List(lngListLoop)
        End If
    Next lngListLoop
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    '''''''''''''''''''''''''''''''''''''''''''''
    For Each rngCell In Range("D10:SP10")
        If InStr(1, strMostar, "|" & Trim(rngCell.Value)) Then
            If Not rngUnion Is Nothing Then
                Set rngUnion = Application.Union(rngUnion, rngCell)
            Else
                Set rngUnion = rngCell
            End If
        End If
    Next rngCell
    If Not rngUnion Is Nothing Then
        Range("D10:SP10").EntireColumn.Hidden = True
        rngUnion.EntireColumn.Hidden = False
    End If
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0
    Thanks Excel Fox,

    The code works perfectly.
    Congratulations on your wonderful forum.

    Grettings

Similar Threads

  1. Maximum columns in a ListBox
    By Rasm in forum Excel Help
    Replies: 7
    Last Post: 05-04-2020, 12:44 PM
  2. VBA To Hide And Unhide Rows
    By paul_pearson in forum Excel Help
    Replies: 10
    Last Post: 05-08-2013, 03:14 AM
  3. Change listbox value
    By Tony in forum Excel Help
    Replies: 4
    Last Post: 12-04-2012, 08:58 PM
  4. Hide and Unhide Rows and Columns
    By Admin in forum Download Center
    Replies: 0
    Last Post: 05-11-2011, 12:00 AM
  5. Replies: 2
    Last Post: 05-06-2011, 02:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •