Macro for Hiding two rows if Cell has specific statement?

donggri84

New Member
Joined
Jul 31, 2013
Messages
18
Hello,

I've been looking online to see what macro I can use for when:

e.g.
B8 = "New Zealand" and "Select"

Hide Rows 22 and 23

otherwise keep it unhidden or If B8 = "Australia", unhidden

Could anyone possibly help me?

Thank you so kindly,
It would be a huge help!
Kind regards,
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
TRY

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Err:
If Application.Intersect(Target, Range("B8")) = "New Zealand" Then
    Rows("22:23").EntireRow.Hidden = True
End If

If Application.Intersect(Target, Range("B8")) = "Australia" Then
    Rows("22:23").EntireRow.Hidden = False
End If
Err:

End Sub
 
Upvote 0
  • Right-click on the sheet tab
  • Select View Code from the pop-up context menu
  • Paste the code from below in the worksheet's code module.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_Change([COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
    [COLOR=darkblue]If[/COLOR] Target.Address(0, 0) = "B8" [COLOR=darkblue]Then[/COLOR]
        Rows("22:23").Hidden = (Target.Value <> "New Zealand" And Target.Value <> "Select")
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
TRY

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Err:
If Application.Intersect(Target, Range("B8")) = "New Zealand" Then
    Rows("22:23").EntireRow.Hidden = True
End If

If Application.Intersect(Target, Range("B8")) = "Australia" Then
    Rows("22:23").EntireRow.Hidden = False
End If
Err:

End Sub

Thank you so much!
Worked great :D

Kind regards
 
Upvote 0
Thank you it works well! it was other way around but I can work it out.
Thank you so so much for your kind help!
Regards,
D
 
Upvote 0
Hello again,

I just realized Macro does not work if the sheet is password protected.
I'm not quite sure where I should be adding below statement if I have to?

ActiveSheet.Unprotect "abc"


Thank you again
Regards
D
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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