VBA: hide rows automatically based on cell text

TVhelo

New Member
Joined
Apr 23, 2021
Messages
2
Office Version
  1. 2016
Hi,

I am new to the VBA but would like to do simple thing with VBA: when specific cell has certain text on it, rows below are unhidden and when cell has something else those rows are hidden.
This macro should work automatically without running the macros manually.

I have tried several things but nothing seems to do the trick. Perhaps the issue is with VBA options, I do not know.

This is an example what I am trying to accomplish: When user selects Yes in cell C4 then row 6 is visible. Otherwise row 6 should be hidden.

Would be really pleased to have some assistance!

1619177484478.png


1619178024934.png



Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C4").Value = "Yes" Then
Rows("6:6").EntireRow.Hidden = False
Else
Rows("6:6").EntireRow.Hidden = True

End If
End Sub
 

Attachments

  • 1619177448829.png
    1619177448829.png
    22.4 KB · Views: 3
  • 1619177894374.png
    1619177894374.png
    40 KB · Views: 2
  • 1619177952315.png
    1619177952315.png
    38.3 KB · Views: 2

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,346
Office Version
  1. 365
Platform
  1. Windows
Welcome to the Board!

It looks like that you have placed this code in a standard modue ("Module1").
Event procedure code, which is code that runs automatically upon some event happening, needs to be placed in the proper workbook or worksheet module.
So you need to move this code to the "Sheet1" module. Then it should fire automatically when cells are manually updated.
 
Solution

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,346
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
:)
 

Forum statistics

Threads
1,136,802
Messages
5,677,814
Members
419,722
Latest member
Rizzol

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
Top