Coding for multiple Hide Columns based on cell value on different worksheet

verze

New Member
Joined
May 27, 2015
Messages
5
Hi all,

Could you please assist me in adding multiple hide columns to this code that uses a cell value to control columns on another worksheet

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    Dim Value As Range, Cells As Range, Cell As Variant
    If Not Intersect(Target, Range("$B$8")).Value = 0 Then
        Sheets("Sheet1").Columns("D:E").EntireColumn.Hidden = False
    Else
        Sheets("Sheet1").Columns("D:E").EntireColumn.Hidden = True
    End If


End Sub

I have multiple cell values ranging from B8 to B20 (there are also gaps in between) and each cell value controls two columns.

I'd like it so that if any of them are zero/blank, that the connected two columns are hidden. The code above works for one but I dont know how to add more.. It was put into the sheets code

Would greatly appreciate some help
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
See if this helps.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    Dim Value As Range, Cells As Range, Cell As Variant
    If Not Intersect(Target, Range("$B$8")).Value = 0 Then
        Sheets("Sheet1").Columns("D:E").EntireColumn.Hidden = False
    Else
        Sheets("Sheet1").Columns("D:E").EntireColumn.Hidden = True
    End If
    If Not Intersect(Target, Range("$B$9")).Value = 0 Then
        Sheets("Sheet1").Columns("F:G").EntireColumn.Hidden = False
    Else
        Sheets("Sheet1").Columns("F:G").EntireColumn.Hidden = True
    End If
End Sub

Luke
 
Upvote 0
Unfortunately that wont work either... Comes up with object variable or with block variable not set?
 
Upvote 0
Here is code for Worksheet_SelectionChange that is working for me.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Range("B8").Value = 0 Then
        Columns("D:E").EntireColumn.Hidden = True
    Else
        Columns("D:E").EntireColumn.Hidden = False
    End If
    
    If Range("B9").Value = 0 Then
        Columns("I:J").EntireColumn.Hidden = True
    Else
        Columns("I:J").EntireColumn.Hidden = False
    End If
    
End Sub
 
Upvote 0
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Range("B8").Value = 0 Then
        Sheets("Sheet2").Columns("D:E").EntireColumn.Hidden = True
    Else
        Sheets("Sheet2").Columns("D:E").EntireColumn.Hidden = False
    End If
    
    If Range("B9").Value = 0 Then
        Sheets("Sheet2").Columns("I:J").EntireColumn.Hidden = True
    Else
        Sheets("Sheet2").Columns("I:J").EntireColumn.Hidden = False
    End If
    
End Sub
 
Upvote 0
That worked perfectly! thanks very much. I had read that worksheet_selectionchange wasnt good so didn't use it... glad you proved me wrong.
 
Upvote 0

Forum statistics

Threads
1,203,234
Messages
6,054,278
Members
444,714
Latest member
excel2782

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