title: Hiding multiple columns in Excel based on the value of a cell
body: <p>I am trying to get columns to hide in a worksheet based on the value in a certain cell. This value could range from 1 to 30, and different columns are hidden for each possible option. I have the following code, but it doesn't seem to work all the time, and then sometimes it only hides some of the columns in the range, but not all (like if 5 was in cell AA1, then sometimes AN:AR will hide and AS:FC will not be hidden). If anyone has any ideas, I would appreciate the help. Thanks!</p>
  
  <p>I have a sample workbook at: <a href="https://drive.google.com/file/d/0B8qPItN2DU0BZ3B0LW1XUS1BMFk/view?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0B8qPItN2DU0BZ3B0LW1XUS1BMFk/view?usp=sharing</a></p>
  
  <pre><code>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Range("AA1").Value = 1 Then
      Columns("O:FC").EntireColumn.Hidden = True
  Else
      Columns("O:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 2 Then
      Columns("T:FC").EntireColumn.Hidden = True
  Else
      Columns("T:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 3 Then
      Columns("Y:FC").EntireColumn.Hidden = True
  Else
      Columns("Y:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 4 Then
      Columns("AD:FC").EntireColumn.Hidden = True
  Else
      Columns("AD:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 5 Then
      Columns("AI:FC").EntireColumn.Hidden = True
  Else
      Columns("AI:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 6 Then
      Columns("AN:FC").EntireColumn.Hidden = True
  Else
      Columns("AN:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 7 Then
      Columns("AS:FC").EntireColumn.Hidden = True
  Else
      Columns("AS:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 8 Then
      Columns("AX:FC").EntireColumn.Hidden = True
  Else
      Columns("AX:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 9 Then
      Columns("BC:FC").EntireColumn.Hidden = True
  Else
      Columns("BC:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 10 Then
      Columns("BH:FC").EntireColumn.Hidden = True
  Else
      Columns("BH:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 11 Then
      Columns("BM:FC").EntireColumn.Hidden = True
  Else
      Columns("BM:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 12 Then
      Columns("BR:FC").EntireColumn.Hidden = True
  Else
      Columns("BR:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 13 Then
      Columns("BW:FC").EntireColumn.Hidden = True
  Else
      Columns("BW:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 14 Then
      Columns("CB:FC").EntireColumn.Hidden = True
  Else
      Columns("CB:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 15 Then
      Columns("CG:FC").EntireColumn.Hidden = True
  Else
      Columns("CG:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 16 Then
      Columns("CL:FC").EntireColumn.Hidden = True
  Else
      Columns("CL:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 17 Then
      Columns("CQ:FC").EntireColumn.Hidden = True
  Else
      Columns("CQ:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 18 Then
      Columns("CV:FC").EntireColumn.Hidden = True
  Else
      Columns("CV:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 19 Then
      Columns("DA:FC").EntireColumn.Hidden = True
  Else
      Columns("DA:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 20 Then
      Columns("DF:FC").EntireColumn.Hidden = True
  Else
      Columns("DF:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 21 Then
      Columns("DK:FC").EntireColumn.Hidden = True
  Else
      Columns("DK:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 22 Then
      Columns("DP:FC").EntireColumn.Hidden = True
  Else
      Columns("DP:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 23 Then
      Columns("DU:FC").EntireColumn.Hidden = True
  Else
      Columns("DU:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 24 Then
      Columns("DZ:FC").EntireColumn.Hidden = True
  Else
      Columns("DZ:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 25 Then
      Columns("EE:FC").EntireColumn.Hidden = True
  Else
      Columns("EE:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 26 Then
      Columns("EJ:FC").EntireColumn.Hidden = True
  Else
      Columns("EJ:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 27 Then
      Columns("EO:FC").EntireColumn.Hidden = True
  Else
      Columns("EO:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 28 Then
      Columns("ET:FC").EntireColumn.Hidden = True
  Else
      Columns("ET:FC").EntireColumn.Hidden = False
  End If
  
  If Range("AA1").Value = 29 Then
      Columns("EY:FC").EntireColumn.Hidden = True
  Else
      Columns("EY:FC").EntireColumn.Hidden = False
  End If
  
  End Sub
  </code></pre>