Solved

Excel User Defined Function to Change Locked Status of Range

Posted on 2012-04-08
6
393 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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