Button to lock and unlock cell

data808

Active Member
Joined
Dec 3, 2010
Messages
354
Office Version
  1. 2019
Platform
  1. Windows
Trying to create some VBA code to lock and unlock cell when a button is clicked on. So far this is what I have and nothing is happening:

VBA Code:
If Range("A1").Locked = True Then
    Selection.Locked = False
    Selection.FormulaHidden = False
End If
    
If Range("A1").Locked = False Then
    Selection.Locked = True
    Selection.FormulaHidden = False
End If

End Sub

Anyone know why? Thanks.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You have selection (which can be Cell A1, but can be any cell that you click on . . . Selection = ActiveCell when the selection is just one cell). I assume that you will separate these two statements into two separate buttons, but this is what I believe you're trying to do. The With statement is like factoring just like how x^2+x = x(x+1).

So the following code will lock/unlock Range("A1").
VBA Code:
With Range("A1")
    If .Locked = True Then
        .Locked = False
        .FormulaHidden = False
    End If
End With

With Range("A1")
    If .Locked = False Then
        .Locked = True
        .FormulaHidden = False
    End If
End With

End Sub
 
Upvote 0
Solution
You have selection (which can be Cell A1, but can be any cell that you click on . . . Selection = ActiveCell when the selection is just one cell). I assume that you will separate these two statements into two separate buttons, but this is what I believe you're trying to do. The With statement is like factoring just like how x^2+x = x(x+1).

So the following code will lock/unlock Range("A1").
VBA Code:
With Range("A1")
    If .Locked = True Then
        .Locked = False
        .FormulaHidden = False
    End If
End With

With Range("A1")
    If .Locked = False Then
        .Locked = True
        .FormulaHidden = False
    End If
End With

End Sub
Thanks. I figured it out. You were right about the selection cell of any cell that I click on. Here is what worked for me but I'm sure yours would have worked too:

VBA Code:
If Range("A1").Locked Then
    Range("A1").Locked = False
    Selection.FormulaHidden = False
    Exit Sub
End If
    
If Range("A1").Locked = False Then
    Range("A1").Locked = True
    Selection.FormulaHidden = False
End If

End Sub
 
Upvote 0
Or even:

VBA Code:
    Range("A1").Locked = Not Range("A1").Locked
    Selection.FormulaHidden = False
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,760
Members
449,336
Latest member
p17tootie

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