Dynamic Range - When new rows are inserted

SUR1984

New Member
Joined
Sep 7, 2017
Messages
6
Hi,
I've been searching to find a solution for my problem for about a week now. Can you please help me on this.

I've a vba code to hide a number of rows based on a selection (Yes or No) in a cell. I've another code to add rows in a table above those rows. Every time a row is added in a table, the range to hide rows has to change. Can it be made dynamic? I'm a novice at vba so please help.

Code to Hide rows is:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B13").Value = "No" Then
With Sheets("Main Sheet")
.Rows("32:40").EntireRow.Hidden = True
End With
End If

If Range("B13").Value = "Yes" Then
With Sheets("Main Sheet")
.Rows("32:40").EntireRow.Hidden = False
End With
End If


Need to update the bold reference automatically.

Need to submit my report tomorrow so very anxious for your response.
Thanks in advance
Umair
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You'll need to keep track of the first row something like this:

Code:
Private firstRow As Long
Private Sub Worksheet_Change(ByVal Target As Range)


If firstRow = 0 Then firstRow = 32


With Sheets("Main Sheet")
    .Range(.Cells(firstRow, 1), .Cells(firstRow + 8, 1)).EntireRow.Hidden = (Range("B13").Value = "No")
End With


End Sub

In the code that adds new rows, simply add the number of rows to firstRow when you do so. Also, you'll need to duplicate this line in your other code:

Code:
If firstRow = 0 Then firstRow = 32

WBD
 
Upvote 0
Thanks a lot for the prompt response.
I've tried an alternative solution and it worked. Previously, I wasn't sure if excel updates the name range when a new row is inserted, but it does.
So, I defined the Range 32:40 by a name and used this name in the code.

If Range("B15").Value = "No" Then
With Sheets("Main Sheet")
.Range("Post_Bid_Area").EntireRow.Hidden = True
End With
End If

Thanks again. Will try your code as well.
 
Upvote 0
If B15 can only contain either "Yes" or "No" then you don't need to check both conditions. You could use:

Code:
Sheets("Main Sheet").Range("Post_Bid_Area").EntireRow.Hidden = (Range("B15").Value = "No")

WBD
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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