+ Reply to Thread
Results 1 to 4 of 4

VBA lock a portion of the worksheet based on cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-18-2013
    Location
    Utah
    MS-Off Ver
    Excel 2010
    Posts
    7

    VBA lock a portion of the worksheet based on cell value

    Hey Everyone,

    Im running into a little bit of a problem. I have a cell set up with an if statement that will return the value 1 or 0. What I am trying to accomplish is that once the value becomes 1 it will lock a portion of the worksheet. if the value were to change back to 0 it would then unlock the portion of the worksheet. Here is the code I am working with. I am no longer recieving any errors, but the code doesnt appear to be doing anything. Any suggestions would be greatly appreciated. Thanks!

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Select Case [v25]
        Case 1:
        If ActiveCell.Value = 1 Then
        Range("B7:N70") = Target
            Target.Locked = True
            Application.EnableEvents = True
                   Me.Protect ("ppppp")
        
        Else
        Select Case [v25]
        Case 0:
        If ActiveCell.Value = 0 Then
            Target.Locked = False
            Me.Unprotect ("ppppp")
            Application.EnableEvents = False
                     
          
         End If
            
            
            End Select
        End If
        End Select
    End Sub

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: VBA lock a portion of the worksheet based on cell value

    You seem to be using the Target range for both a function level argument and then a different range.

    You could try using a different name

    e.g.

    Dim Target2 as Range
    .......
    
    Set Target2 =  Range("B7:N70")
    Martin

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,027

    Re: VBA lock a portion of the worksheet based on cell value

    Try:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("V25")) Is Nothing Then Exit Sub
        ActiveSheet.Unprotect Password:="ppppp"
        Select Case Target.Value
            Case Is = 1
                Range("B7:N70").Locked = True
            Case Is = 0
                Range("B7:N70").Locked = False
        End Select
        ActiveSheet.Protect Password:="ppppp"
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA lock a portion of the worksheet based on cell value

    Hello inklejt,

    You could also consider a non VBA approach by using Data Validation, as per the attached sample Workbook.

    Regards.
    Attached Files Attached Files
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

+ 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 row based on cell value in same row
    By Eline in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2013, 09:56 AM
  2. How to lock worksheet based on the current date
    By Dan Vollmer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-20-2012, 09:35 PM
  3. cut a portion of a cell's contents and paste the cut portion into the net column
    By NJEvan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-12-2010, 05:08 AM
  4. Replies: 2
    Last Post: 12-06-2008, 02:41 AM
  5. Worksheet lock based on time
    By coolanks in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-30-2008, 03:30 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