+ Reply to Thread
Results 1 to 8 of 8

VBA lock cells based on their content

Hybrid View

  1. #1
    Registered User
    Join Date
    08-06-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    68

    VBA lock cells based on their content

    Hello everyone! Because my VBA skills are almost non-existent, I don't know how to go about doing what I need to do.
    So basically, I have a workbook that will be used by multiple users. There is a sheet with an excel table in it. Some columns have formulas and some need the user to type in data. As I need to protect the formulas in the sheet, I've locked the cells with formulas and kept the rest unlocked so I can protect the sheet.

    The trouble is that in one of the columns (column Q) I have a formula which will return either a number or, if there is an error in the formula, it will return the text "Please input value". So I need this column to be locked except for cells where there is the text "Please input value". I need this formula to keep recalculating after any change in this sheet so I cannot lock the cell since the result might change.

    I came up with a code to protect the sheet with a password by adding a button on the sheet (so the user cannot change the password when protecting the sheet). The button can protect and unprotect the sheet. This is my code (I also wanted the caption on the button to change depending on whether the sheet is protected or not):

    Private Sub ToggleButton1_Click()
        ToggleButton1.Caption = "Unprotect"
        If ActiveSheet.ProtectContents = True Then
            On Error GoTo Message
            ActiveSheet.Unprotect
            ToggleButton1.Caption = "Protect"
        Else
            ToggleButton1.Caption = "Unprotect"
            ActiveSheet.Protect "Password123", AllowFiltering:=True, AllowSorting:=True
           
        End If
        Exit Sub
    Message:
            MsgBox ("Incorrect password entered. Please try again")
    End Sub
    I'm unsure if I can integrate additional code for locking cells in the above since the above code only runs when clicking on the Protect/Unprotect button and I actually need a macro to run all the time, I think, to pick up on any changes that might alter the result in cells in column Q. I also don't know if this will make the workbook significantly slower or not. But any help is appreciated. Thank you

    PS:I posted this on MrExcel as well but no luck there with finding a solution.
    Last edited by Sinon05; 11-30-2015 at 06:22 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA lock cells based on their content

    Hi,

    I don't understand. If the column Q cell is returning the message about inputting a value because the cell is currently returning an error, then presumably you want the precedent cells to the Q cell to be changed so that the formula does return a non error value. I'm not clear why protecting/unprotecting is involved
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    08-06-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    68

    Re: VBA lock cells based on their content

    Have a look at the attached. In the "Data" sheet, in column Q, you can see the simplified formula of what I am actually using. The entire table is simplified as my actual table has sensitive data in it and a lot more formulas. Because there are a lot of users using this spreadsheet, I need to protect the formulas. Once I protect the sheet though, i cannot add new lines in this table (as Excel does not allow this) so I added additional lines below so new information can be added (as in rows 7,8,9)
    The formula in Q returns the commission rate based on who the commission is paid to (in column P) from a table in another spreadsheet (commission table sheet). Not everyone has a commission rate though and some commission amounts are provided to the user, with no further calculation necessary. For these cases, the user needs to be able to manually type the amount in, over the formula.
    So, when new lines are completed, depending on who the commission needs to be paid to, the formula in column Q will return either the commission amount or the text "Please input value". This is why I need these cells to be constantly checked for text and unlocked/unprotected if text is present.

    Does that make more sense?
    Attached Files Attached Files

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA lock cells based on their content

    Hi Sinon05

    Place this Code in the Sheet Module for Sheet Data...it has some hitches in it's giddyup...see if it works for you.

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       Dim LR           As Long
       Dim Comm         As Double
       LR = Range("Q" & Rows.Count).End(xlUp).Row
       If Not Intersect(Target, Range("Q2:Q" & LR)) Is Nothing Then
          Select Case Target.Value
          Case "Please input value"
             On Error Resume Next
             Application.DisplayAlerts = False
             Comm = Application.InputBox _
                    (Prompt:="Please enter Commission.", _
                     Title:="ENTER COMMISSION", Type:=1)
             On Error GoTo 0
             Application.DisplayAlerts = True
             If Comm = 0 Then
                Exit Sub
             Else
                ActiveSheet.Unprotect
                Target.Value = Comm
                ActiveSheet.Protect
             End If
          End Select
       End If
    End Sub
    ***Please Note Well...if this is a Shared File ALL BETS are off...
    Attached Files Attached Files
    Last edited by jaslake; 11-26-2015 at 07:33 PM. Reason: ???Shared File???
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Registered User
    Join Date
    08-06-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    68

    Re: VBA lock cells based on their content

    Oh this works wonderfully. Thank you so much. My workbook will not be shared so that isn't an issue. I am expecting users to go in it "one at a time" to add lines and the rest can go in it Read-Only. There is one thing I did notice though. If I make a selection containing a cell from Q (like selecting multiple cells, including one from column Q) I get an error
    Run-time error '13':
    Type mismatch.

    The debugger takes me to the following line in the code:
    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       Dim LR           As Long
       Dim Comm         As Double
       LR = Range("Q" & Rows.Count).End(xlUp).Row
       If Not Intersect(Target, Range("Q2:Q" & LR)) Is Nothing Then
          Select Case Target.Value
          Case "Please input value"
             On Error Resume Next
             Application.DisplayAlerts = False
             Comm = Application.InputBox _
                    (Prompt:="Please enter Commission.", _
                     Title:="ENTER COMMISSION", Type:=1)
             On Error GoTo 0
             Application.DisplayAlerts = True
             If Comm = 0 Then
                Exit Sub
             Else
                ActiveSheet.Unprotect
                Target.Value = Comm
                ActiveSheet.Protect
             End If
          End Select
       End If
    End Sub

    I am lost to be honest. A quick search of the error did not help but make it look more complicated. Any ideas?
    Thanks

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA lock cells based on their content

    Hi Sinon05

    Add the Line of Code as indicated.
    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       Dim LR           As Long
       Dim Comm         As Double
       
       If Target.Cells.Count > 1 Then Exit Sub  '<---Add this line of Code
       
       LR = Range("Q" & Rows.Count).End(xlUp).Row
       If Not Intersect(Target, Range("Q2:Q" & LR)) Is Nothing Then
          Select Case Target.Value
          Case "Please input value"
             On Error Resume Next
             Application.DisplayAlerts = False
             Comm = Application.InputBox _
                    (Prompt:="Please enter Commission.", _
                     Title:="ENTER COMMISSION", Type:=1)
             On Error GoTo 0
             Application.DisplayAlerts = True
             If Comm = 0 Then
                Exit Sub
             Else
                ActiveSheet.Unprotect
                Target.Value = Comm
                ActiveSheet.Protect
             End If
          End Select
       End If
    End Sub

  7. #7
    Registered User
    Join Date
    08-06-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    68

    Re: VBA lock cells based on their content

    That did it. Everything is perfect. Thank you so very much

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA lock cells based on their content

    You're welcome...glad I could help. Thanks for the Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Lock/unlock specific cells in a row based on another cell content of the same row
    By st_rod000 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2014, 02:58 AM
  2. Automatically copy cells content based on the content of another cell.
    By chrisbarlow1984 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-01-2014, 11:13 AM
  3. Replies: 3
    Last Post: 06-28-2012, 09:05 AM
  4. Lock cells based on value of other cells - code simplification query
    By glenin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-07-2009, 03:11 AM
  5. Lock cells so content or formula can't be changed
    By Rayan83 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-14-2008, 09:01 AM
  6. Lock and clear cell content based on another cell's value
    By mohitmahajanin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2008, 06:30 AM
  7. [SOLVED] How do I lock a cell based on content of another cell?
    By Alan T in forum Excel General
    Replies: 3
    Last Post: 10-23-2006, 08:57 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1