Solved

Excel User Defined Function to Change Locked Status of Range

Posted on 2012-04-08
6
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 34

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

717 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