View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_3_] Dave Peterson[_3_] is offline
external usenet poster
 
Posts: 2,824
Default Protect sheet while enabling outlines and custom views

You could combine ranges:

Dim myRng1 As Range
Dim myRng2 As Range

Set myRng1 = Me.Range("AGRegionsProtected1")
Set myRng2 = Me.Range("AGRegionsProtected2")

If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Union(myRng1, myRng2)) Is Nothing Then Exit Sub


==
You could even define a third range manually that's the combination of both of
these ranges:

Insert|range|define
Names in workbook:
AGRegionsProtectedAll

Refers to:
=AGRegionsProtected1,AGRegionsProtected2

You won't see it in the namebox dropdown, but you can type it in and select the
combined range.




wrote:

Tight as Fort Knox! Thanks for the help!

One problem I ran into (it's fixed) is that the total number of cells
I had to include was too long for the Insert ... Name ... Define
dialog, so I had to define two different ranges and put a loop in the
code as such:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRng As Range
Set myRng = Me.Range("AGRegionsProtected1")

On Error GoTo errHandler:

If Intersect(Target, myRng) Is Nothing Then
' Check whether they're trying to edit AGRegionsProtected2
Set myRng = Me.Range("AGRegionsProtected2")
If Intersect(Target, myRng) Is Nothing Then
' let 'em do it. target is in neither 1 nor 2.
Else
' Undoing edit on AGRegionsProtected2 range
With Application
.EnableEvents = False
.Undo
End With
MsgBox "This is a protected range filled with formulas
only. Editing not allowed.", vbCritical, "ERROR! DO NOT EDIT!"
End If
Else
' Undoing edit on AGRegionsProtected1 range
With Application
.EnableEvents = False
.Undo
End With
MsgBox "This is a protected range filled with formulas only.
Editing not allowed.", vbCritical, "ERROR! DO NOT EDIT!"
End If
errHandler:
Application.EnableEvents = True

End Sub

This was my first attempt at "real" code. Thanks for the help, Dave.

The only other thing I'm going to try to figure out is how to disable
editing/deleting (tampering with) my defined name ranges. There's got
to be a way to disable that on workbookopen; I'm open to tips. Other
than that -- assuming the user enables macros -- I think I'm pretty
covered.

Again, I can't say how appreciative I am.

Regards,
Michael

P.S. Is there a good place I can reference for definitions? For
example, I see lots of folks start their code with Dim, but I don't
know what it is and can't seem to find anywhere that defines it.

Dave Peterson wrote in message ...
Mmmmmm.

First, oops. I got the code for each routine in the wrong one.

The code for the worksheet_Change was under _selectionchange (and vice versa).
(well, it looks that way today! But maybe someone changed it after I posted. I
blame the gremlins.)

It should have read:

Option Explicit
Private Sub Worksheet_selectionChange(ByVal Target As Range)

Dim myRng As Range
Set myRng = Me.Range("AGRegionsProtected")

If Intersect(Target, myRng) Is Nothing Then
'let 'em do it
Else
Application.EnableEvents = False
Me.Range("a1").Select
Application.EnableEvents = True
MsgBox "Please don't get near that radio active range!"
End If

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRng As Range
Set myRng = Me.Range("AGRegionsProtected")

On Error GoTo errHandler:

If Intersect(Target, myRng) Is Nothing Then
'let 'em do it
Else
With Application
.EnableEvents = False
.Undo
End With
MsgBox "I've asked you not to change this range!"
End If

errHandler:
Application.EnableEvents = True

End Sub

But I think I would only use one of them (the current! worksheet_change if I had
my choice.)

Sorry about my confusion.

===
When I was testing, the .undo undid my range naming (the last thing I did). It
was sure irritating.



[snip]


--

Dave Peterson