Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Welcome to Stack Overflow. Sometimes, the macro recorder is your friend.</p> <p>I recorded a macro of myself hiding columns E through H. Here was the recorded code:</p> <pre><code>Sub Macro1() ' ' Macro1 Macro ' Macro recorded 7/17/2012 by Jimmy Peña ' ' Columns("E:H").Select Selection.EntireColumn.Hidden = True End Sub </code></pre> <p>The Select/Selection pair is a clear indication that I can concatenate these two lines. I shorten the code like this:</p> <p><code>Columns("E:H").EntireColumn.Hidden = True</code></p> <p>Since the property takes a value of <code>True</code>, I know that I can hide columns by setting the value to <code>True</code> and show columns by setting the value to <code>False</code>.</p> <p>Since you haven't shown the code you are using, I'll provide a generic approach which you will need to customize for your use.</p> <p>I added a listbox to a sample userform. This code will selectively hide/unhide depending on which value is selected.</p> <pre><code>Private Sub ListBox1_Click() Dim selectedValue As String ' get selected value from listbox selectedValue = ListBox1.value Application.ScreenUpdating = False ' unhide any columns Cells.EntireColumn.Hidden = False Select Case selectedValue Case "JAN" Columns("E:H").EntireColumn.Hidden = True Case "FEB" Columns("I:M").EntireColumn.Hidden = True Case "MARCH" Columns("N:R").EntireColumn.Hidden = True Case "APRIL" Columns("S:W").EntireColumn.Hidden = True Case "MAY" Columns("X:AB").EntireColumn.Hidden = True Case "JUNE" Columns("AC:AG").EntireColumn.Hidden = True End Select Application.ScreenUpdating = True End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload