Auto Hide & Unhide Rows in Excel

Anonymous
2019-10-30T10:31:02+00:00

Hi, All.

I have to create an excel spreadsheet where the next 2 rows stay hidden if the answer is No or N/A or blank.

In the sample below:

* Column A with item number (1,2,3....) has a drop down list of ("Yes"or "No"or "N/A") in column C. I used Data Validation for this.

* Rows with no item number should stay hidden (rows 10,11,13,14,16&17) until "Yes" has been selected from the drop down list in column C.

* If the item in column C (C9) has no answer (or blank), rows 10 & 11 should stay hidden - I hide the rows manually for this purpose.

* Items with "No"or "N/A" has been selected from the drop down list in column C, the next 2 rows should stay hidden. Rows 16 & 17 should stay hidden in this sample.

* If "Yes" has been selected from the drop down list in column C, the content in column B for the next 2 rows (rows 13&14) will appear. 

* Column B has IF formula that auto populates if the answer is "Yes".

I will really appreciate your assistance on this.

Thank you in advance!

Regards,

Shaynne

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2019-10-30T13:02:18+00:00

    Sorry, I forgot an End If:

    Private Sub Worksheet_Change(ByVal Target As Range)

        Dim rng As Range

        If Not Intersect(Range("C:C"), Target) Is Nothing Then

            Application.ScreenUpdating = False

            Application.EnableEvents = False

            Me.Unprotect Password:="secret"

            For Each rng In Intersect(Range("C:C"), Target)

                If rng.Value = "Yes" Then

                    rng.Offset(1).Resize(2).EntireRow.Hidden = False

                    rng.Offset(1).Resize(2).EntireRow.AutoFit

                Else

                    rng.Offset(1).Resize(2).EntireRow.Hidden = True

                End If

            Next rng

            Me.Protect Password:="secret"

            Application.EnableEvents = True

            Application.ScreenUpdating = True

        End If

    End Sub

    1 person found this answer helpful.
    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2019-10-30T11:07:01+00:00

    Right-click the sheet tab.

    Select 'View Code' from the context menu.

    Copy the following code into the worksheet module:

    Private Sub Worksheet_Change(ByVal Target As Range)

        Dim rng As Range

        If Not Intersect(Range("C:C"), Target) Is Nothing Then

            Application.ScreenUpdating = False

            Application.EnableEvents = False

            For Each rng In Intersect(Range("C:C"), Target)

                rng.Offset(1).Resize(2).EntireRow.Hidden = (rng.Value <> "Yes")

            Next rng

            Application.EnableEvents = True

            Application.ScreenUpdating = True

        End If

    End Sub

    Switch back to Excel.

    Save the workbook as a macro-enabled workbook (.xlsm).

    Make sure that you allow macros when you open the workbook.

    0 comments No comments
  2. Anonymous
    2019-10-30T11:40:28+00:00

    Hi, Hans.

    Thank you so much for the prompt response.

    When I tried to run the code for the first it worked but when I tried to protect the sheet, the next 2 rows didn't appear when I selected yes.

    Kind Regards,

    Shaynne

    0 comments No comments
  3. HansV 462.4K Reputation points MVP Volunteer Moderator
    2019-10-30T11:55:16+00:00

    If the sheet is protected, you have to unprotect it temporarily in the code:

    Private Sub Worksheet_Change(ByVal Target As Range)

        Dim rng As Range

        If Not Intersect(Range("C:C"), Target) Is Nothing Then

            Application.ScreenUpdating = False

            Application.EnableEvents = False

            Me.Unprotect Password:="secret"

            For Each rng In Intersect(Range("C:C"), Target)

                rng.Offset(1).Resize(2).EntireRow.Hidden = (rng.Value <> "Yes")

            Next rng

            Me.Protect Password:="secret"

            Application.EnableEvents = True

            Application.ScreenUpdating = True

        End If

    End Sub

    Here, "secret" is the password used to protect the sheet. If you didn't specify a password, use "".

    0 comments No comments
  4. Anonymous
    2019-10-30T12:32:56+00:00

    Thank you, again Hans.

    Sorry for asking too many question but how do I resize the rows to fit the contents in column B.

    Regards,

    Shaynne

    0 comments No comments