ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   formating cells from IF statement (https://www.excelbanter.com/excel-programming/421549-formating-cells-if-statement.html)

Anner

formating cells from IF statement
 
I want to protect a given cell if the contents of another cell is "Conf". I
can record a macro to Format-Cell-Protection-Locked, but how do I combine
that with the conditional IF? Thanks very much!


JLGWhiz

formating cells from IF statement
 
I don't believe you can protect just one cell the way you described. You
have to protect the entire sheet and then unlock those cells that you want to
allow editing in.

"Anner" wrote:

I want to protect a given cell if the contents of another cell is "Conf". I
can record a macro to Format-Cell-Protection-Locked, but how do I combine
that with the conditional IF? Thanks very much!


Rick Rothstein

formating cells from IF statement
 
Here is a different approach for you to consider. You didn't tells us
anything about your layout, so I made the assumption that A1 was the cell
you wanted protected if C3 contained the text "Conf" (change them to the
cells you are actually using. Put this code in the worksheet's code window
(right click that worksheet's tab and select View Code)...

'***************** START OF CODE *****************
Dim OldValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C3").Value = "Conf" And Target.Address = "$A$1" Then
On Error GoTo Done
Application.EnableEvents = False
MsgBox "That cell cannot be changed if A1 equals ""Conf""!"
Target.Value = OldValue
Target.Select
Else
'
' Your Change event code, if any, goes here
'
End If
Done:
Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then OldValue = Target.Value
'
' Your SelectionChange event code, if any, goes here
'
End Sub
'***************** END OF CODE *****************

--
Rick (MVP - Excel)


"Anner" wrote in message
...
I want to protect a given cell if the contents of another cell is "Conf".
I
can record a macro to Format-Cell-Protection-Locked, but how do I
combine
that with the conditional IF? Thanks very much!




All times are GMT +1. The time now is 10:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com