PDA

View Full Version : Hide/Unhide Columns with Listbox



obed_cruz
05-28-2011, 04:20 AM
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.

Excel Fox
05-28-2011, 09:53 AM
Hi obed_cruz

Welcome to ExcelFox

Use this in the userform


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

obed_cruz
05-28-2011, 07:26 PM
Thanks Excel Fox,

The code works perfectly.
Congratulations on your wonderful forum.

Grettings