Marco not working after inserting new rows

1q2w3

New Member
Joined
Mar 20, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi all,
Please can anyone help with this?
I've used the following but if I insert a new row before the selected rows (i.e. before row 16), the code then doesn't work anymore. How can I make sure the code is locked to the cell I selected?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("F14"), Range(Target.Address)) Is Nothing Then
    Select Case Target.Value
    Case Is = "A":
                Rows("16:32").EntireRow.Hidden = True 
    Case Is = "B":
                Rows("16:19").EntireRow.Hidden = False
                Rows("20:32").EntireRow.Hidden = True
    Case Is = "C":
                Rows("16:19").EntireRow.Hidden = True
                Rows("20:31").EntireRow.Hidden = False
                Rows("32").EntireRow.Hidden = True
    Case Is = "D":
                Rows("16:31").EntireRow.Hidden = True
                Rows("32").EntireRow.Hidden = False
    End Select
End If
 
Last edited by a moderator:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Target, Range("F14")) Is Nothing Then

if target.count > 1 then exit sub

Select Case Target.Value
Case Is = "A":
Rows("16:32").EntireRow.Hidden = True
Case Is = "B":
Rows("16:19").EntireRow.Hidden = False
Rows("20:32").EntireRow.Hidden = True
Case Is = "C":
Rows("16:19").EntireRow.Hidden = True
Rows("20:31").EntireRow.Hidden = False
Rows("32").EntireRow.Hidden = True
Case Is = "D":
Rows("16:31").EntireRow.Hidden = True
Rows("32").EntireRow.Hidden = False
End Select
End If
 
Upvote 0
This doesn't seem to work, when I add a new row - 'F14' didn't response to hiding the selected rows.
Any other ideas?
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block at the bottom of this post has more details. I have added the tags for you this time. 😊

Try this (post back if you need help with any of it)
  1. Give cell F14 a name. I used "DVcell" since I guessed you might have those letters as Data Validation choices in that cell.

  2. Give, say, F16:F32 a name. This doesn't have to be column F but any range that includes all those rows. I used the name "myRows".

  3. Now try this worksheet change code.
The code should continue to work no matter what rows are added/deleted provided the ""DVcell" and "myRows" named ranges exist (and "myRows" has at least 17 rows).

Note that I have added an extra section that unhides all the rows if some other value appears in the "DVcell" or if that cell is cleared.
If you don't want that section then just remove the two relevant lines

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Application.Intersect(Target, Range("DVcell")) Is Nothing Then
    With Range("myRows").EntireRow
      Select Case Range("DVcell").Value
        Case Is = "A"
          .Rows("1:17").Hidden = True
        Case Is = "B"
          .Rows("1:4").Hidden = False
          .Rows("5:17").Hidden = True
        Case Is = "C"
          .Rows("1:4").Hidden = True
          .Rows("5:16").Hidden = False
          .Rows(17).Hidden = True
        Case Is = "D"
          .Rows("1:16").Hidden = True
          .Rows(17).Hidden = False
        Case Else
          .Hidden = False
      End Select
    End With
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,897
Messages
6,187,708
Members
453,435
Latest member
U4US

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