Hiding Columns Based on a Changing Cell Range

ERed1

Board Regular
Joined
Jun 26, 2019
Messages
104
Hi everyone,
I am trying to write a code that hides columns based off of a cell range. So if "Dog" is anywhere in B18 to B25, then columns N:X will disappear.

Here is my code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B18:B25").Value = "Dog" Then
Columns("N:X").EntireColumn.Hidden = False
Else
Columns("N:X").EntireColumn.Hidden = True
End If
End Sub

Even if I use just cell B18, columns N:X still disappear when Dog is entered, when it should not be disappearing.

I'm sure this is pretty simple, but I have been stuck on it for about a day and need some help. Thanks for the help!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi & welcome to MrExcel
Do you want to hide the columns if the range contains Dog, or unhide them?
 
Upvote 0
In that case how about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("B18:B25")) Is Nothing Then
      If Application.CountIf(Range("B18:B25"), "Dog") > 0 Then
         Columns("N:X").EntireColumn.Hidden = False
      Else
         Columns("N:X").EntireColumn.Hidden = True
      End If
   End If
End Sub
 
Upvote 0
In that case how about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("B18:B25")) Is Nothing Then
      If Application.CountIf(Range("B18:B25"), "Dog") > 0 Then
         Columns("N:X").EntireColumn.Hidden = False
      Else
         Columns("N:X").EntireColumn.Hidden = True
      End If
   End If
End Sub

That worked perfectly! Thank you!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
So I added Cat to the problem instead of Dog. Ranges are Z:AB. whenever cat is entered, the Dog columns appear too. Any solution for this?
 
Upvote 0
What code are you using?
 
Upvote 0
The one you made, except I switched the Dog to Cat and the columns from N:X to Z:AB
 
Upvote 0
I think I know why It is doing that. I have a button to bring information in from another sheet and the way I have it pasting it unhides the columns
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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