PDA

View Full Version : [SOLVED:] Protect Workbook (Lock Certain Cells) and still have VBA run on unlocked cells??



MOC
05-25-2021, 04:58 PM
So, next problem:
On the linked Workbook, I want to LOCK the following:

Rows 1, 2 & 3
Columns A, J, K, M, O, P & R
The remaining Columns must be UNLOCKED.

Now, I know how to do this, and it works perfectly, except... None of the VBA works when I lock it.



So, what am I missing in my VBA code to make the VBA code work ONLY on the unlocked cells?

Thank you in advance!

FILE IS HERE (https://drive.google.com/file/d/1yU4rT5LmUGpf1OwO0x5FYji9G2BT1Fwq/view?usp=sharing)

This is the code:



Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strList As String
On Error Resume Next
Application.EnableEvents = False


Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Not Intersect(Target, rngDV) Is Nothing Then
If Target.Validation.Type = 3 Then
strList = Target.Validation.Formula1
strList = Right(strList, Len(strList) - 1)
strDVList = strList
frmDVList.Show
If Target.Value = "HIGH" Then
Call PostMitChoice_Initialize
End If
If Target.Value = "CATASTROPHIC" Then
Call PostMitChoice_Initialize
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub


Private Sub Worksheet_Change1(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = ", "
Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler


Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If newVal = "" Then
'do nothing
Else
If oldVal = "" Then
Target.Value = newVal
Else
Target.Value = oldVal & strSep & newVal
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub


Private Sub PostMitChoice_Initialize()
Load PostMitChoice
PostMitChoice.Show
End Sub

Paul_Hossler
05-25-2021, 07:13 PM
Thank you in advance!

FILE IS HERE (https://drive.google.com/file/d/1yU4rT5LmUGpf1OwO0x5FYji9G2BT1Fwq/view?usp=sharing)

This is the code:



Click [Go Advanced] bottom right, and then use the paper clip icon to attach the file to your post instead of some file sharing site

And I still think it should be



Private Sub Worksheet_Change(ByVal Target As Range)


without the "1"

MOC
05-26-2021, 10:19 AM
I took the "1" out ... :friends:

I tried attaching the file and get this error:
28546

MOC
05-26-2021, 10:37 AM
Solved at: https://www.mrexcel.com/board/threads/protect-workbook-lock-certain-cells-and-still-have-vba-run-on-unlocked-cells.1172057/

THANK YOU!!!