+ Reply to Thread
Results 1 to 2 of 2

Using VBA to lock cell c based on info in cell b

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2008
    Location
    CA
    Posts
    64

    Using VBA to lock cell c based on info in cell b

    Hi! The linked thread shows almost exactly what I'm looking to accomplish.
    http://www.mrexcel.com/forum/showthread.php?p=1572477
    (My apologies for finding it on another forum, it came up in a search... I'm really very loyal to this forum cause the folks here rock way harder than anywhere else...)

    But as I know very little about VBA, I have a few questions...

    I have two different sites entering information onto a spreadsheet. If the first site enters information in Column B, I need column C locked so the second site doesn't write try entering useless information. If the first site does not enter information, column C stays unlocked.

    Column B does not actually contain numerical values. It contains references to numerical values from another workbook.


    So the following is what VBA Noob suggested in the other thread:

    Originally Posted by VBA Noob
    Try an event macro > right click sheet tab > select view code > paste in the below.
    If A1 formula returns 10 then the cell is locked.

    Private Sub Worksheet_Calculate()
    Dim Rng As Range
    Set Rng = Range("A1")
    
    Select Case Rng
    Case Is = 10
    Rng.Locked = True
    Case Else
    Rng.Locked = False
    End Select
    End Sub
    HTH
    VBA Noob
    So… how could I make the following changes and, is the fact that there is a reference to another workbook rather than an actual numerical value going to make a difference?
    And lastly, how to I apply this to all the rows, not just C1 & B1, but to C2 & B2 all the way down?


    Private Sub Worksheet_Calculate()
    Dim Rng As Range
    Set Rng = Range("C1")
    
    Select Case Rng
     Case Is = B1 is Greater than 0 or Is not blank (whichever would work better)
         Rng.Locked = True
     Case Else
         Rng.Locked = False
    End Select
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Using VBA to lock cell c based on info in cell b

    This will lock or unlock C? whenever B? is changed. And will only execute when someone changes something in column B (not everytime the sheet is calculated).
    Private Sub Worksheet_Change(ByVal Target As Range)
        if Target.Cells.Count = 1 then
            if not intersect(Target,$B$B) is nothing then
                'someone changed something in column $B
                Target.offset(0,1).locked = Not IsEmpty(target.value)
            end if
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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