Consulting

Results 1 to 9 of 9

Thread: Protect Individual Cell with Formula?

  1. #1
    VBAX Regular golf4's Avatar
    Joined
    Jul 2004
    Location
    Salem, OR
    Posts
    54
    Location

    Protect Individual Cell with Formula?

    Hi, everyone -

    I just had a quick query. Working on my income calculation tool, I ran into something I haven't tried before. First, my data entry sheet is fully protected, via password, except for cells I use to enter specific data for calculation.

    What I'm wondering is what the best method is to accomplish the following (example): if cell A1 equals either 4 or 5, then how would I protect cell A2? I'm thinking it's got to be something like
    =IF(OR(A1=4,A1=5),PROTECT(A2),"")
    ?

    Or could the same thing be accomplished in Conditional Formatting? Any help would be great.


    Thanks,

    Golf

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You can get tricky with validation, but the cell won't really be protected. What you need is VBA. Put this code in the sheet code section for the sheet you want it to work on. Change "Sheet1" to the actual sheet name, you can also use ActiveSheet if you prefer.


    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Range("A1").Value = 4 Or Range("A1").Value = 5 Then
            Sheets("Sheet1").Unprotect Password:="Password"
            Range("A2").Locked = True
            Sheets("Sheet1").Protect Password:="Password"
        Else
            Sheets("Sheet1").Unprotect Password:="Password"
            Range("A2").Locked = False
            Sheets("Sheet1").Protect Password:="Password"
        End If
    End Sub

  3. #3
    VBAX Regular golf4's Avatar
    Joined
    Jul 2004
    Location
    Salem, OR
    Posts
    54
    Location
    Hi, Jacob -
    Thanks for the response. I really appreciate your suggestion.

    Take care,
    Golf

  4. #4
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    http://www.mrexcel.com/board2/viewto...=500582#500582

    In general I would prefer that OP at least let the boards know that the same question has been asked on several boards so people who respond are aware of it.

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Quote Originally Posted by golf4
    Hi, Jacob -
    Thanks for the response. I really appreciate your suggestion.

    Take care,
    Golf
    You're Welcome

    Take Care

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi Golf,

    Why not just skip the VBA and use Data Validation? You can use Custom, formula ...

    =((A1<>4)+(A1<>5))<>1

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    That's what I meant by
    Quote Originally Posted by DRJ
    You can get tricky with validation, but the cell won't really be protected
    While the validation will stop you from typing into the cell, it unfortunately won't stop you from clearing the cell contents or from copy/paste.

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Good point Jake. I was just thinking along the lines that simple may be better. If not, it's just another option for Frank.

  9. #9
    VBAX Regular golf4's Avatar
    Joined
    Jul 2004
    Location
    Salem, OR
    Posts
    54
    Location
    Thanks, again, Jake and Zack -

    KISS ( ya gotta love it!!!). I really appreciate all of the ideas. I had also posted my query on www.mrexcel.com (thread posted below) where I kind of expounded a little more of my actual situation. I figure I could always use my exposure to VBA, so I decided to go that way. Plus I figure I could always "cannibalize" the code I have now for other things. I've used Validation in so many other places in the tool as it is that Staff is getting real used to just "entering" through the warning message boxes without reading them.

    http://www.mrexcel.com/board2/viewto...2946&highlight=

    Dennis - Thanks for the suggestion.

    Anyway, I really appreciate all the help from everyone.

    Take care,

    Frank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •