+ Reply to Thread
Results 1 to 4 of 4

Locking cells with a checkbox using a macro.

  1. #1
    SplatterKat
    Guest

    Locking cells with a checkbox using a macro.

    Hello. I have an ongoing problem. I am trying to lock cells so they
    cannot be changed as long as a checkbox on the active sheet is checked.
    I have some code, but it runs an error and doesn't quite work anyway.
    I have another macro that fills the boxes with values, but I want the
    users to be able to adjust those values. Only after the checkbox is
    checked, I want the access to those cells to be locked. Here is a
    sampling of the code I have so far:

    Sub chkLockAttribs_Click()
    '
    'Private Sub chkLockAttribs_Click()


    Dim myRng As Range
    Dim myPwd As String


    myPwd = "secret"


    Set myRng = ActiveSheet.Range("C18, D19:D23, D25, D26:F26, J19:J23,
    P33, P35")


    ActiveSheet.Unprotect Password:=myPwd
    myRng.Locked =
    ActiveSheet.OLEObjects("chkLockAttribs").Object.Value
    ActiveSheet.cmdRoll.Visible = Not
    (ActiveSheet.chkLockAttribs.Value)
    ActiveSheet.Protect Password:=myPwd



    End Sub

    The checkbox is chkLockAttribs and the button linked to a macro
    generating values is cmdRoll. C18, D19:D23, D25, D26:F26, J19:J23,
    P33, P35 is the range of cells I want to lock after the checkbox is
    checked. I don't really need a password protection on this. It's more
    a convienence thing.

    So any suggestions? Any and all help appreciated.

    -SplatterKat-

    aka -Robert Lizak-


  2. #2
    Dave Peterson
    Guest

    Re: Locking cells with a checkbox using a macro.

    I put a checkbox from the control toolbox toolbar on the worksheet. I named it
    "chklockattribs".

    I put a commandbutton from the control toolbox toolbar on that same worksheet.
    I named it "cmdRoll".

    I doubleclicked on that checkbox and pasted this into the code window that
    opened:

    Option Explicit
    Private Sub chkLockAttribs_Click()

    Dim myRng As Range
    Dim myPwd As String

    myPwd = "secret"

    Set myRng = Me.Range("C18, D19:D23, D25, D26:F26, J19:J23,P33, P35")
    Me.Unprotect Password:=myPwd
    myRng.Locked = Me.chkLockAttribs.Value
    Me.cmdRoll.Visible = Not (Me.chkLockAttribs.Value)
    Me.Protect Password:=myPwd

    End Sub


    It worked fine for me.

    If you used shapes from the Forms toolbar, make sure you include that info in
    any followup.

    SplatterKat wrote:
    >
    > Hello. I have an ongoing problem. I am trying to lock cells so they
    > cannot be changed as long as a checkbox on the active sheet is checked.
    > I have some code, but it runs an error and doesn't quite work anyway.
    > I have another macro that fills the boxes with values, but I want the
    > users to be able to adjust those values. Only after the checkbox is
    > checked, I want the access to those cells to be locked. Here is a
    > sampling of the code I have so far:
    >
    > Sub chkLockAttribs_Click()
    > '
    > 'Private Sub chkLockAttribs_Click()
    >
    > Dim myRng As Range
    > Dim myPwd As String
    >
    > myPwd = "secret"
    >
    > Set myRng = ActiveSheet.Range("C18, D19:D23, D25, D26:F26, J19:J23,
    > P33, P35")
    >
    > ActiveSheet.Unprotect Password:=myPwd
    > myRng.Locked =
    > ActiveSheet.OLEObjects("chkLockAttribs").Object.Value
    > ActiveSheet.cmdRoll.Visible = Not
    > (ActiveSheet.chkLockAttribs.Value)
    > ActiveSheet.Protect Password:=myPwd
    >
    > End Sub
    >
    > The checkbox is chkLockAttribs and the button linked to a macro
    > generating values is cmdRoll. C18, D19:D23, D25, D26:F26, J19:J23,
    > P33, P35 is the range of cells I want to lock after the checkbox is
    > checked. I don't really need a password protection on this. It's more
    > a convienence thing.
    >
    > So any suggestions? Any and all help appreciated.
    >
    > -SplatterKat-
    >
    > aka -Robert Lizak-


    --

    Dave Peterson

  3. #3
    Registered User
    Join Date
    09-08-2010
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Locking cells with a checkbox using a macro.

    I have a sheet protected and want to have a checkbox to enable the user to essentially unlock a particular cell and override number. Here is the macro I have created for my checkbox.

    I keep getting an error at:

    Sub CheckBox111_Click()

    Range("K34").Select
    If CheckBox111.Value = True Then
    Selection.Locked = False
    Else
    Selection.Locked = True

    If CheckBox111.Value = False Then
    Selection.Locked = True
    Else
    Selection.Locked = False
    End If

    End Sub

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 2000/3/7/10/13/16/365
    Posts
    52,123

    Re: Locking cells with a checkbox using a macro.

    Hi Jean, welcome to the forum

    I hate to do this on your very first post with us, but you went wrong at least 2 different ways (take a quick peek at the forum rules for these http://www.excelforum.com/forum-rule...rum-rules.html

    1st, according to Rule 3 you need to use code tags around code.
    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    2nd, Unfortunately your post does not comply with Rule 2 of our Forum , Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    (This thread is almost 10 years old)

    Thanks
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ 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