Protect - Unprotect code location

U

usmc-r70

I have the following code that works except for the ActiveSheet.Unprotect and
ActiveSheet.Protect (no password to be used), I suspect that I do not have it
in the proper arrangement / location. Can someone help?

Option Explicit
Sub RANKING_SORT()
ActiveSheet.Unprotect
Dim source As Range
With ActiveSheet
Set source = .Range(.Range("A11"), .Range("AR11").End(xlDown))
End With
source.Sort source.Range("V11"), xlAscending
ActiveSheet.Protect
End Sub
 
U

usmc-r70

The 'unprotect' works, but the 'protect' does not. If I manually 'protect'
the sheet, the code will 'unprotect' and execute the code, but will not
'protect'. I recorded a macro of 'protecting' and 'unprotecting' the
worksheet and added the additional code to the:
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

But still no 'protect'. Any ideas?

Option Explicit
Sub RANKING_SORT()
ActiveSheet.Unprotect
Dim source As Range
With ActiveSheet
Set source = .Range(.Range("A11"), .Range("AR11").End(xlDown))
End With
source.Sort source.Range("V11"), xlAscending
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
 
P

Per Jessen

Hi

It worked fine for me in both Excel 2000 and Excel 2007.

You could try to step through the code with F8 and check if all statements
are executed.

Regards,
Per
 
U

usmc-r70

I stepped through as you suggested with the following results:
OK Sub RANKING_SORT()
OK ActiveSheet.Unprotect
SKIPPED Dim source As Range
OK With ActiveSheet
OK Set source = .Range(.Range("A11"), .Range("AR11").End(xlDown))
OK End With
OK source.Sort source.Range("V11"), xlAscending
NO ACTION ActiveSheet.Protect
UNKNOWN End Sub
 
U

usmc-r70

I removed a specific module and its code: i.e. the following:
Function HasFormula(c As Range) As Boolean
HasFormula = c.HasFormula
End Function

And the aforementioned code works, do you know why?
 
P

Per Jessen

Hi

The code that you posted can not prevent the sheet from beeing protected.
But maybe this UDF is called from an event code in the sheet module or in
ThisWorkbook module ?

My guess is that you have some event code which unprotect the sheet but
never protect it again.

//Per
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top