Solved

Excel User Defined Function to Change Locked Status of Range

Posted on 2012-04-08
6
392 Views
Last Modified: 2012-04-09
I am trying to auto-(un)lock a range based on a value in another column.  As a simple example I have three cells and am trying to use a user defined function to solve instead of a long loop cycle.

D5: is the value that drives the change (1=unlock,else=lock)
E5: UDF (see code below)
G5: range to lock

On page UDF first
=LockIt(D5,"G5")

Open in new window


The module level function
Public Function LockIt(ByVal nlValue As Integer, ByVal rangeIN As String) As Boolean
On Error GoTo ErrHandler:

    Dim lLock As Boolean
    Dim lSuccess As Boolean
    
    If nlValue = 1 Then
        lLock = False
    Else
        lLock = True
    End If
    
     Sheet1.range(rangeIN).Locked = lLock

    Debug.Print Sheet1.range(rangeIN).Locked
    
    LockIt = lLock

ErrCleanup:
    LockIt = lSuccess
    Exit Function
ErrHandler:
    lSuccess = False
    MsgBox Err.Description, vbExclamation + vbOKOnly, "Error Encountered"
    Resume ErrCleanup
End Function

Open in new window


The code is running without error, but the range lock status is not happening.  

Thanks.
0
Comment
Question by:cadwal01
6 Comments
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 37822287
"The code is running without error, but the range lock status is not happening. "

do you mean that part of the selection structure is not being hit when it you expect that it should?
0
 
LVL 33

Accepted Solution

by:
Norie earned 400 total points
ID: 37822301
You can't use UDF's to change the worksheet, eg formatting, protection, they can only be used to return values.

Note, I've seen some examples that kind of bend this rule, if that's the way to put it, but not too many.
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 37822313
that's good to know, imnorie. I've been trying to troubleshoot this code to work but with no success. No doubt because UDFs are not meant to accomplish this sort of task.

cadwal01, maybe you need to do this with a user-triggered macro or with the worksheet_change event.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Assisted Solution

by:suvmitra
suvmitra earned 100 total points
ID: 37822555
Work with Allow User to Edit Ranges.

By default worksheet is locked. The only way of unlocking a portion to the user to work with "Allow User to Edit Ranges"

The sheet must be protected with a Password.

The code needs to

1. Unlock the sheet with password (if it is protected)
2. Allow user to edit certail ranges
3. Lock the Worksheet with the password.
0
 

Author Closing Comment

by:cadwal01
ID: 37822877
Unfortunately, and I forgot to mention, I had to develop this in 2003 b/c of client environment.  Kind of figured this would be the answer.  Appreciate the insights.
0
 
LVL 33

Expert Comment

by:Norie
ID: 37823081
It's not a version thing as far as I know, you've never been able to use VBA UDFs for this sort of thing.

Also, I'm afraid you can't really use the Change event - it's not triggered by value of a formula changing.

If you wanted to use the Change event I suppose you could monitor other cells, but that might not be practical if you don't know where the UDF will be used and what cell references might be used in it.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question