VBA: Hide columns based on 2 conditions

djecris

New Member
Joined
Jan 4, 2022
Messages
10
Hi all!

I have a limited knowledge of VBA and I'm learning by trial and error, but this time I'm really stuck.

I would like to hide columns based on 2 conditions.

example, conditions here are A1 and B1:
if A1 = 2022 and B1 = 2023, then we hide columns R to W
if A1 = 2023 and B1 = 2025, then we hide columns I to N and U to W
etc.

The macro would be triggered as soon as a value is entered in B1.

Could someone give a solution to my problem?
 

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
Welcome to the Board!

Right-click on the sheet tab name at the bottom of the screen, select View Code, and paste this VBA code in the VB Editor window:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Only run if cell B1 is updated
    If Intersect(Range("B1"), Target) Is Nothing Then Exit Sub
   
'   First unhide all columns
    Columns("I:W").Hidden = False
   
'   Check values of A1 and B1
    If Range("A1") = 2022 And Range("B1") = 2023 Then
        Columns("R:W").Hidden = True
    End If
    If Range("A1") = 2023 And Range("B1") = 2025 Then
        Columns("I:N").Hidden = True
        Columns("U:W").Hidden = True
    End If
   
End Sub
It should do what you want automatically, as you manually update the value in cell B1.
 
Upvote 0
Solution
You are welcome!
Glad I was able to help.

I added some documentation to the code to show you what each step is doing.
Let me know if you have any questions about it.
 
Upvote 0
You are welcome!
Glad I was able to help.

I added some documentation to the code to show you what each step is doing.
Let me know if you have any questions about it.
Thank you!

Sorry to ask, I have 2 follow-up questions, hoping you can answer:

1) You have put the modification of B1 as a trigger for the macro with "If Intersect(Range("B1"), Target) Is Nothing Then Exit Sub"
Is there a simple way to also have A1 as a trigger so that if either A1 or B1 is modified, the macro starts? I've tried to directly add a similar line below with A1 and to include A1 directly in the line you've written but neither is working.

2) I don't know it that's something simple to do but:
How can I add a third condition (in addition to A1 and B1) but with as a text and not a number?
For example:
if A1 = 2022 and B1 = 2023 and C1 = "Marketing", then we hide columns R to W
 
Upvote 0
1. Change this line:
VBA Code:
    If Intersect(Range("B1"), Target) Is Nothing Then Exit Sub
to this:
VBA Code:
    If Intersect(Range("A1:B1"), Target) Is Nothing Then Exit Sub
then it will run on any change to A1 or B1.

2. If you notice in my original code, there are two distinct IF...THEN blocks, one for each of your sets of conditions.
You can add more blocks like that for you other checks.
Give it a try, and see how you do, and post back if you run into issues.
 
Upvote 0
1. Change this line:
VBA Code:
    If Intersect(Range("B1"), Target) Is Nothing Then Exit Sub
to this:
VBA Code:
    If Intersect(Range("A1:B1"), Target) Is Nothing Then Exit Sub
then it will run on any change to A1 or B1.

2. If you notice in my original code, there are two distinct IF...THEN blocks, one for each of your sets of conditions.
You can add more blocks like that for you other checks.
Give it a try, and see how you do, and post back if you run into issues.
It's working thank you ! Sorry for the question, I was sure that with a text variable it would have complexified the macro, but it's working fine!
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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