VBA Hide columns

blossomthe2nd

Active Member
Joined
Oct 11, 2010
Messages
450
Hi

I have a spread sheet has columns A-Z populated with headers.

I wish to write a code that will highlight certain columns based on a drop down in cell B1, see below.

*Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B1").Value = "Red" Then
Columns("H:Z").EntireColumn.Hidden = True
Else
Columns("H:Z").EntireColumn.Hidden = False
End If
End Sub
*

This is working fine, but there are a few different combinations i need to use.

Is it possible to amend the above to state if range is equal to RED OR BLUE OR GREEN hide H:Z ?

And if so How ?

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Have a look at the instr() function.

Code:
if instr("red blue green",lcase(range("b1").value))>0 then
  'do things here
end if

Should work
 
Upvote 0
Thanks Weaver,

Can I ask ? Some of the drop downs are 2 words like Light green, would this look like ---

if instr("red blue green light green",lcase(range("b1").value))>0 then
'do things here
end if

Or do I need _ ?

Thanks
A
 
Upvote 0
If possible, I'd be tempted to go with the underscore, especially if say you didn't want the columns to hide for "green" but you did for "light green". You'll have to change the dropdowns as well although I'm sure you already realised this. If this is a problem, there are other ways of achieving the same result.
 
Upvote 0
Hi Weaver ,

I've tried the folloing and it isnt working ?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If InStr("Out_Of_Course Drawdowns Customer_Service", LCase(Range("B2").Value)) > 0 Then
Columns("C:G").EntireColumn.Hidden = True
Else
Columns("C:G").EntireColumn.Hidden = False
End If
End Sub


I'm very new to this -- Have any advice ?
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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