VBA and creating Dynamic Ranges

ACarrasco

New Member
Joined
Sep 28, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
I have a spreadsheet with multiple tables within one sheet.

I have crated a VBA code that allows a) multiple selections within acell from a drop down list and b) show/hides rows depending on the option selected in a different cell.

The VBA code functions correctly, however the issue I am running into is when a row is added above the row range for the show/hide code. Although I have defined the range in the Name Manager, it does not shift the row number auotomatically. I need the code to change dynamically when a row is added or deleted.

Here is the VBA code I am using:
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$B$33" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
If Not Application.Intersect(Range("B35"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "No": Range("36:37,TRACK").EntireRow.Hidden = True
Range("91:91").EntireRow.Hidden = False
Case Is = "Yes": Range("91:91").EntireRow.Hidden = True
Range("36:37,TRACK").EntireRow.Hidden = False

End Select
End If
End Sub

Here is the Defined Name Range:
1601318035753.png


Thanks for the help!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Track adjusts, can you name 36:37 and 91:91 as something as well?
 
Upvote 0
Track adjusts, can you name 36:37 and 91:91 as something as well?
Yes, I can rename the two ranges. I merged these two VBA codes thatI found on the Internet so 91:91 is a row that I don't need to hide/show but couldn't make the code function without having it in it.

My issue comes when rows 76-90 are hidden (based on cell value in B35) and I add a row to in my table above row 76. the hidden rows are shifted down and become visible. So when a row is added I need my Track table to adjust to eg. 77-91 etc.

thanks for the help.
 
Upvote 0
Yes, I renamed both of them. Inserted a new row and its working now.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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