Consulting

Results 1 to 8 of 8

Thread: Unprotect and protect after macro run

  1. #1
    VBAX Regular
    Joined
    Jul 2005
    Posts
    8
    Location

    Unprotect and protect after macro run

    Hello,
    I'm attempting to create a user friendly worksheet for our staff. However I'm running into a problem I hope someone can help me with. I have a macro assigned to a check box that bolds the indicated line when the box is checked and removes the bold when the box is not checked. However I require the sheet to be protected to keep the users from deleting rows or columns, so that my totals that are linked in other workbooks aren't compromised.
    When the sheet is protected I get an erroor 1004, when I attempt to use the checkboxes with the assigned macro.
    Someone pointed out that it is possible to turn off and on protection within the macro. I was wondering how this is done.
    Here is what I have so far that I wish to add the protection off and on to.

    Sub CheckBox4_Click()
    If Range("L4").Value =  True Then
    Range("A4:H4").Select
    Selection.Font.Bold = True
    Else
    If  Range("L4").Value = False Then
    Range("A4:H4").Select
    Selection.Font.Bold =  False
    End If
    End If
    End Sub

    Thanks for any help you can give me.
    Cheers,
    Greg.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Presumably, you have the checkbox linked to cell L4? That is a problem, as the error will occur when you check the box.

    What sort of checkbox is it, Forms or Control Toiolbox (I am assuming the former). If it Forms, unlink it, and use this code


    Sub CheckBox4_Click()
        With ActiveSheet
            .Unprotect
             Range("A4:H4").Font.Bold = .CheckBoxes(Application.Caller).Value = xlOn
            .Protect
        End With
    End Sub

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hi welcome to the board. You can use this at the beginning of the macro:

    ActiveSheet.Unprotect
    and at the very end:

    ActiveSheet.Protect
    Peace of mind is found in some of the strangest places.

  4. #4
    VBAX Regular
    Joined
    Jul 2005
    Posts
    8
    Location
    thank you for your quick responses,
    I went with the second solution, because I need the FALSE to be entered into the L4 cell so that it translates over via the link in another document to tell that line how to behave (in the other document).
    Thanks again!

  5. #5
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    If your problem is solved, please mark your thread solved. You can do that via Thread Tools right above your first post.
    Peace of mind is found in some of the strangest places.

  6. #6
    VBAX Regular
    Joined
    Jul 2005
    Posts
    8
    Location
    thanks for the tip.

  7. #7
    VBAX Regular
    Joined
    Jul 2005
    Posts
    30
    Location
    You can also add password protection by entering

    .unprotect("1234") with 1234 as password(or any value you choose)

    Jim

  8. #8
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Good tip. Didn't know that. Thanks
    Peace of mind is found in some of the strangest places.

Posting Permissions

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