Error in Code to Hide Columns - Help Identify Issue

thejmiller

New Member
Joined
Apr 26, 2013
Messages
3
I want to hide a column if a cell in the column has a 0 in a designated cell (binary coded via vlookup). I want to run this 9 times, for column E through column L. What am I doing wrong that is causing the error?

Sub HideColumnE()


If Cell("E5").Value = 0 Then
Columns("E").EntireColumn.Hidden = True
Else
Columns("E").EntireColumn.Hidden = False
End If

Sub HideColumnF()


If Cell("F5").Value = 0 Then
Columns("F").EntireColumn.Hidden = True
Else
Columns("F").EntireColumn.Hidden = False
End If


Sub HideColumnG()
If Cell("G5").Value = 0 Then
Columns("G").EntireColumn.Hidden = True
Else
Columns("G").EntireColumn.Hidden = False
End If


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
At first glace, it looks like you only ended the HideColumnG Sub. You need to End Sub before beginning the next.
 
Upvote 0
Here is my revised code:

Sub HideColumnD()
If Cell("D5").Value = 0 Then
Columns("D").EntireColumn.Hidden = True
Else
Columns("D").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnE()
If Cell("E5").Value = 0 Then
Columns("E").EntireColumn.Hidden = True
Else
Columns("E").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnF()
If Cell("F5").Value = 0 Then
Columns("F").EntireColumn.Hidden = True
Else
Columns("F").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnG()
If Cell("G5").Value = 0 Then
Columns("G").EntireColumn.Hidden = True
Else
Columns("G").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnH()
If Cell("H5").Value = 0 Then
Columns("H").EntireColumn.Hidden = True
Else
Columns("H").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnI()
If Cell("I5").Value = 0 Then
Columns("I").EntireColumn.Hidden = True
Else
Columns("I").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnJ()
If Cell("J5").Value = 0 Then
Columns("J").EntireColumn.Hidden = True
Else
Columns("J").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnK()
If Cell("K5").Value = 0 Then
Columns("K").EntireColumn.Hidden = True
Else
Columns("K").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnL()
If Cell("L5").Value = 0 Then
Columns("L").EntireColumn.Hidden = True
Else
Columns("L").EntireColumn.Hidden = False
End If
End Sub

I am getting a "compile error: sub or function not defined" and it is only highlighted on my final section...
 
Upvote 0
Change the word Cell to Range.

Sub HideColumnD()
If Range("D5").Value = 0 Then
Columns("D").EntireColumn.Hidden = True
Else
Columns("D").EntireColumn.Hidden = False
End If
End Sub
Sub HideColumnE()
If Range("E5").Value = 0 Then
Columns("E").EntireColumn.Hidden = True
Else
Columns("E").EntireColumn.Hidden = False
End If
End Sub
etc.
 
Upvote 0
this should get you there:

Code:
Sub HideColumns()
Dim i As Long
    For i = 5 To 12
        If Cells(5, i).Value = 0 Then
            Cells(5, i).EntireColumn.Hidden = True
        If Cells(5, i) = "" Then
            Cells(5, i).EntireColumn.Hidden = False
        End If
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,440
Messages
6,136,635
Members
450,022
Latest member
Joel1122331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top