VBA code to hide row if two or more conditions are met

liabilityquek

New Member
Joined
Oct 17, 2021
Messages
25
Office Version
  1. 2019
Platform
  1. Windows
Hi All

I am trying to work on a vba code to hide rows if 2 or more criteria have been met.

If Cell1 contains "Govt Agencies & Stat Board" and Cell2 contains "Yes", I would need to hide rows 5 rows
If Cell1 contains "Govt Agencies & Stat Board" and Cell2 contains "No", I would need to hide rows 5 rows
If Cell1 contains "Govt Agencies & Stat Board" and Cell2 contains "Credit Re-assessment", I would need to hide rows 5 rows

else
rows will not be hide

Thanks in advance!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi

I know this is a similar question, but i would avoid asking different questions on an existing thread marked with a solution.
In the future i would suggest starting a new thread otherwise since this marked as solved, it wont get allot of interest.

But in any case i believe the below may work for you.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$5" Then
    Columns("AG:AI").EntireColumn.Hidden = False
    If Range("D5") = "Februarie" Then Columns("AG:AI").EntireColumn.Hidden = True
    If Range("D5") = "Aprilie" Then Columns("AI").EntireColumn.Hidden = True
    If Range("D5") = "Iunie" Then Columns("AI").EntireColumn.Hidden = True
    If Range("D5") = "Septembrie" Then Columns("AI").EntireColumn.Hidden = True
    If Range("D5") = "Noiembrie" Then Columns("AI").EntireColumn.Hidden = True
    If Range("D5") = "" Then Columns("AG:AI").EntireColumn.Hidden = False
    End If
End Sub
Hello,

I checked the worksheet today and for example when I open it all the rows are unhidden and if I write "Februarie" (February) in cell D5 rows AG:AI become hidden so this works and after that if I write another month in D5 that do not need any rows to be hidden for example "August" then the rows become unhidden so it works so far but when I delete the text and cell D5 contains nothing, rows AG:AI remain hidden and I do not understand why. Can you help please?
 
Upvote 0
Hi

I have just checked my code, and i coded in that scenarion. As a secondary check, i phisically checked my code and it works for me.

Do you have any other code running that may be stopping the sheets from calculating?

Dave
 
Upvote 0
Hi

I have just checked my code, and i coded in that scenarion. As a secondary check, i phisically checked my code and it works for me.

Do you have any other code running that may be stopping the sheets from calculating?

Dave
I also have the code below but meanwhile, before you answered I checked for solutions online and I put "Else" before the last row of the code and now it works. I hope it will work everytime :).

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$5" Then
Columns("AG:AI").EntireColumn.Hidden = False
If Range("D5") = "Februarie" Then Columns("AG:AI").EntireColumn.Hidden = True
If Range("D5") = "Aprilie" Then Columns("AI").EntireColumn.Hidden = True
If Range("D5") = "Iunie" Then Columns("AI").EntireColumn.Hidden = True
If Range("D5") = "Septembrie" Then Columns("AI").EntireColumn.Hidden = True
If Range("D5") = "Noiembrie" Then Columns("AI").EntireColumn.Hidden = True
Else
If Range("D5") = "" Then Columns("AG:AI").EntireColumn.Hidden = False
End If
End Sub


This is the other code :

Sub Ascunde()
response = MsgBox("Esti sigur ?", vbYesNo)
If response = vbNo Then
Exit Sub
End If

With Rows("60:139")
If .Hidden Then
.Hidden = False
Else
.Hidden = True
End If
End With
With Rows("29:46")
If .Hidden Then
.Hidden = False
Else
.Hidden = True
End If
End With
End Sub

Thank you for your help!
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,101
Members
449,066
Latest member
Andyg666

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