Updating Target Cell To Hide/Unhide After Deleting Rows

kaybin

New Member
Joined
Jan 24, 2012
Messages
1
Hello,

I am very new to VBA code. I am trying to write a macro where based on the response of a target cell, a number of rows will hide or unhide. Here is the code that I currently have, and it works.

Private Sub Worksheet_Change(ByVal Target As Range)

If Range("C62") = "Yes" Then
Range("63:84").EntireRow.Hidden = False
End If

If Range("C62") = "No" Then
Range("63:84").EntireRow.Hidden = True
End If

End Sub

However, the response cell "C62" is not always constant. I will often delete or add rows above the response cell. What can I add to update my code after a row addition/deletion?

Thanks.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You could name the response cell. Call it "ResponseCell" for example. Then use the name in your code like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("ResponseCell")
    Case "Yes": Rows("63:84").Hidden = False
    Case "No": Rows("63:84").Hidden = True
End Select
End Sub
Excel will keep track of the named cell even if you insert or remove rows above it.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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