Hide Columns Based on Value in a certain Row

05Li139

New Member
Joined
Nov 10, 2016
Messages
8
Hello All,

I'm trying to hide all columns in a range based on a zero value in an =SUM cell at the top of the column.

I've used the following macro to hide rows based on a zero value;

Sub Hide_ZeroYearsLife()
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "K").End(xlUp).Row
On Error Resume Next
For Each c In Range("K12:K" & LastRow)
If c.Value = 0 Then
c.EntireRow.Hidden = True
ElseIf c.Value = 1 Then
c.EntireRow.Hidden = False
End If
Next
On Error GoTo 0
Application.EnableEvents = True
End Sub


And edited it to the following to hide the columns:


Sub Hide_ZeroCountShortfall()
Dim LastColumn As Long, c As Range
Application.EnableEvents = False
LastColumn = Cells(Cells.Columns.Count, "Row10").End(xlUp).Column
On Error Resume Next
For Each c In Range("V10:DE10" & LastColumn)
If c.Value = 0 Then
c.EntireColumn.Hidden = True
ElseIf c.Value = 1 Then
c.EntireColumn.Hidden = False
End If
Next
On Error GoTo 0
Application.EnableEvents = True
End Sub


However I keep getting a debug message based on the red highlighted row


Can anyone advise

Thank you
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You would write that like this:

Code:
LastColumn = Cells(10, Cells.Columns.Count).End(xlToLeft).Column

The cells method requires rows first then columns. So this will produce the last cell in row 10.
 
Upvote 0

Forum statistics

Threads
1,216,419
Messages
6,130,515
Members
449,585
Latest member
kennysmith1

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