Vba confused by formula

manmah

Board Regular
Joined
May 11, 2009
Messages
70
Hi I am using the following VBA to lock a cell:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range("D22")) Is Nothing) And Target.Cells.Count = 1 Then
ActiveSheet.Unprotect
If Target.Value = "Y" Then
Target.Offset(0, 3).Locked = False
Else
Target.Offset(0, 3).Locked = True
End If
ActiveSheet.Protect
End If

If Not (Intersect(Target, Range("A24")) Is Nothing) And Target.Cells.Count = 0 Then
ActiveSheet.Unprotect
If Target.Value = "Y" Then
Target.Offset(0, 3).Locked = False
Else
Target.Offset(0, 3).Locked = True
End If
ActiveSheet.Protect
End If

End Sub

The first part works fine but the second does not work..I have a formula in
A24 as getting "Y" in that cell is based on different scenarios..
I was wondering if there is anything I can do so the VBA does not recognise a formula as an entry into the cell as I presume this is what is causing the problem..

Thanks
Mark
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this

Code:
Private Sub Worksheet_Calculate()
Me.Unprotect
Range("D24").Locked = Range("A24").Value <> "Y"
Me.Protect
End Sub
 
Upvote 0
If there is a formula in A24 you could use
Code:
If Not (Intersect(Target, Range("A24").Precedents) Is Nothing) And Target.Cells.Count = 0 Then
 
Upvote 0
Thanks VoG that worked a treat...
Cheers
Thanks Mike, I had tried the .precedents but for some reason it did not work..
Cheers
Mark
 
Upvote 0
Code:
With Range("A24")
    If Not (Intersect(Target, .Precedents) Is Nothing) Then
        ActiveSheet.Unprotect
        .Offset(0, 3).Locked = (CStr(.Value) <> "Y")
        ActiveSheet.Protect
    End If
End With
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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