Locking of cells with protected sheet not working

Eurekaonide

Active Member
Joined
Feb 1, 2010
Messages
433
Hi All

I think there is probably something very small and simple I am missing here as it did seem to work but now is not.

I have a form with checkboxes on it, when a checkbox is selected it will Lock the cells for editing in the corresponding Column, hide the column off and then when the OK button is pressed it protects the sheet (currently without a password).

For some reason it is now not locking the cells.

VBA Code:
This is an example of the code for the checkboxes
Private Sub CheckBox1_Click()
Sheets("Data").Select
 ActiveSheet.Unprotect
If Me.CheckBox1.Value = True Then
    Sheets("Data").Columns("Q:Q").Hidden = True
    Selection.Locked = True
ElseIf Me.CheckBox1.Value = False Then
    Sheets("Data").Columns("Q:Q").Hidden = False
    Selection.Locked = False
End If

End Sub

and this is the code for the OK button

VBA Code:
Private Sub CommandButton1_Click() 'OK
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingRows:=True, _
        AllowInsertingRows:=True, AllowFiltering:=True, AllowFormattingColumns:=True
       
Unload Me

End Sub

Can you see what I am doing wrong please?
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Which is selected before this line is executed:
VBA Code:
Selection.Locked = True

Artik
 
Upvote 0
The Checkbox is either selected or not and then the OK Button is selected.

I wondered if I need to Lock then Hide?
Ive changed my checkbox code to the below and I think it has resolved it.

VBA Code:
Private Sub CheckBox1_Click()
Sheets("Data").Select
 ActiveSheet.Unprotect
If Me.CheckBox1.Value = True Then
    Sheets("Data").Columns("Q:Q").Locked = True
    Sheets("Data").Columns("Q:Q").Hidden = True
ElseIf Me.CheckBox1.Value = False Then
    Sheets("Data").Columns("Q:Q").Locked = False
    Sheets("Data").Columns("Q:Q").Hidden = False
End If

End Sub
 
Last edited by a moderator:
Upvote 0
Solution
It can be written in shorter form:
VBA Code:
Private Sub CheckBox1_Click()
    With Worksheets("Data")
        .Unprotect
        With .Columns("Q")
            .Locked = Me.CheckBox1.Value
            .Hidden = Me.CheckBox1.Value
        End With
        '.Select 'If necessary
    End With
End Sub

Artik
 
Upvote 0
It can be written in shorter form:
VBA Code:
Private Sub CheckBox1_Click()
    With Worksheets("Data")
        .Unprotect
        With .Columns("Q")
            .Locked = Me.CheckBox1.Value
            .Hidden = Me.CheckBox1.Value
        End With
        '.Select 'If necessary
    End With
End Sub

Artik
Thank you
 
Upvote 0

Forum statistics

Threads
1,224,764
Messages
6,180,839
Members
453,000
Latest member
JAO Esq

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top