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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,223,790
Messages
6,174,594
Members
452,574
Latest member
hang_and_bang

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