Hein;567771 said:
The user must have access to insert rows.
The code that is currently there is as follows:
Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("B3")) Is Nothing Then Exit Sub
Select Case Target.Value
Case "yes"
Application.ScreenUpdating = False
Rows("10:20").Select
Selection.EntireRow.Hidden = False
Application.ScreenUpdating = True
Case "no"
Application.ScreenUpdating = False
Rows("10:20").Select
Selection.EntireRow.Hidden = True
Application.ScreenUpdating = True
End Select
End Sub
Effectively it should probably run with a loop whereby the range B3
will
adjust depending on the amount of rows inserted and the rows selections
in
the case selection must also adjust depending on the rows inserted.
First, and as an aside, your select case code can be shorter:
Application.ScreenUpdating = False
Select Case Target.Value
Case "yes": Rows("10:20").Hidden = False
Case "no": Rows("10:20").Hidden = True
End Select
Application.ScreenUpdating = True
Second, try setting up a named range which, to some extent, will resize
itself when rows are inserted and deleted. Eg. set up a named range
called *TheRange* (Here, you should select a block of cells spanning
rows 10 to 20 inclusive. The number of columns you choose to include in
the range is up to you. Bear in mind that if you choose to make it only
one column wide and a user at some stage might delete that column, you'd
lose your named range, so include several columns which are never going
to be deleted - or select entire rows to be the named range.)
BTW, to name a _new_ range, it's quickest to select that range then
type the name you choose (-TheRange -in the example below) in the -Name
Box- which is usually leftmost in the -Formula bar.-
Then replace your -select case- code with the following:
Application.ScreenUpdating = False
Select Case Target.Value
Case "yes": Range("TheRange").EntireRow.Hidden = False
Case "no": Range("TheRange").EntireRow.Hidden = True
End Select
Application.ScreenUpdating = True
There are a few caveats:
Inserting rows in the middle rows is always OK
Inserting rows at the bottom is OK, but inserting row(s) at row 1 of
the range doesn't expand the range.
Experiment on a blank sheet on which has this code behind it (after
you've set up a named range, say B10:B20, called TestRange in it):
Private Sub Worksheet_Change(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
Range("TestRange").Interior.ColorIndex = 3
End Sub
This will re-highlight the named range everytime you make
changes to the sheet.
--
p45cal
*p45cal*
------------------------------------------------------------------------
p45cal's Profile:
http://www.thecodecage.com/forumz/member.php?userid=558
View this thread:
http://www.thecodecage.com/forumz/showthread.php?t=155819
Microsoft Office Help
.