Sheet code for conditional formatting

MeaclH

Board Regular
Joined
Apr 2, 2014
Messages
71
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
G'day all,
Bit of a curly one. I am hoping to have a sheet code that might be able to achieve the following.

Range of cells may include the following values - EDO or EDO-U
If any of those cells are changed from those values to any value containing a 0, I would like the formatting of that cell to be changed. I can't use a simple conditional formatting rule because there are other cells in the range which may already have a value containing 0, and the formatting needs be different.

Any ideas on if and how this might be able to be achieved?

Cheers
Hayden
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Can you provide a sample of your data and your expected results?
 
Upvote 0
Hi mate, thanks for responding.

This is my current setup, basically I need anything that is EDO or EDO-U that is then changed in the data validation table to a value containing a 0, then the formatting is cell interior colour RGB 0, 176, 240 and font colour RGB 255, 0, 0.

Likewise I would like the same done for any cell that is OFF or OFF-U and is changed to a value containing a 0, then the formatting is cell interior RGB 255, 255, 0 and font colour RGB 255, 0, 0.

Hope that explains what I am after?

Cheers
Hayden
DAO DIGITAL ROSTER.xlsm
GHIJKLMNOPQRST
38OFF1330BC1330PB1330CFD1330PL/SEDOOFFOFF1330BC1330PB1330CFD1330PL/S1330NRNOFF
39
40 13:3013:3013:3013:30 13:3013:3013:3013:3013:30
41 21:4521:4521:4521:45 21:4521:4521:4521:4521:45
42
430530NCC0530PL/SOFFOFF0530NRN0530BC0530PB0530NCC0530PL/SOFFOFF0530CFD0530PL/S0530BC
44
455:305:30 5:305:305:305:305:30 5:305:305:30
4613:4513:45 13:4513:4513:4513:4513:45 13:4513:4513:45
47
MASTER
Cell Formulas
RangeFormula
G40:T40,G45:T45G40=VLOOKUP(G38,DATA!$AF$4:$AH$39,2,FALSE)
G41:T41,G46:T46G41=VLOOKUP(G38,DATA!$AF$4:$AH$39,3,FALSE)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "EDO-U"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "OFF-U"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "BLV"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "CTFN"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "CDO"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "STFN"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "SDO"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "LSL"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "A/L"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "PHC"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "OFF"textNO
F12:T36,F38:T137,F139:T158,F160:T174Cell Valuecontains "EDO"textNO
G39:T39,G42:T42,G44:T44,G47:T47,G49:T49,G52:T52,G54:T54,G57:T57,G59:T59,G62:T62,G64:T64,G67:T67,G69:T69,G72:T72,G74:T74,G77:T77,G79:T79,G82:T82,G84:T84,G87:T87,G89:T89,G92:T92,G94:T94,G97:T97,G99:T99,G102:T102,G104:T104,G107:T107,G109:T109,G112:T112Cell Valuecontains "DEC"textNO
N38:O38Cell ValueduplicatestextNO
G39:T39,G42:T42,G44:T44,G47:T47,G49:T49,G52:T52,G54:T54,G57:T57,G59:T59,G62:T62,G64:T64,G67:T67,G69:T69,G72:T72,G74:T74,G77:T77,G79:T79,G82:T82,G84:T84,G87:T87,G89:T89,G92:T92,G94:T94,G97:T97,G99:T99,G102:T102,G104:T104,G107:T107,G109:T109,G112:T112Cell Valueending with "?"textNO
G41:T41,G15:T15Expression=IF(G$42<>"",TRUE)textNO
G40:T40,G14:T14Expression=IF(G$39<>"",TRUE)textNO
G46:T46,G20:T20Expression=IF(G$47<>"",TRUE)textNO
G45:T45,G19:T19Expression=IF(G$44<>"",TRUE)textNO
G39:T39,G42:T42,G44:T44,G47:T47,G13:T13,G16:T16,G18:T18,G21:T21Cellcontains a blank value textNO
G39:T39,G42:T42,G44:T44,G47:T47,G49:T49,G52:T52,G54:T54,G57:T57,G59:T59,G62:T62,G64:T64,G67:T67,G69:T69,G72:T72,G74:T74,G77:T77,G79:T79,G82:T82,G84:T84,G87:T87,G89:T89,G92:T92,G94:T94,G97:T97,G99:T99,G102:T102,G104:T104,G107:T107,G109:T109,G112:T112Cell Valuecontains "OK"textNO
G5:T5,G156:T157,G151:T152,G146:T147,G141:T142,G135:T136,G130:T131,G125:T126,G120:T121,G115:T116,G110:T111,G105:T106,G100:T101,G95:T96,G90:T91,G85:T86,G80:T81,G75:T76,G70:T71,G65:T66,G60:T61,G55:T56,G50:T51,G45:T46,G40:T41,G29:T30,G24:T25,G19:T20,G14:T15Cellcontains an errortextNO
H40:T40,H50:T50,H55:T55,H60:T60,H65:T65,H70:T70,H75:T75,H80:T80,H85:T85,H90:T90,H95:T95,H100:T100,H105:T105,H110:T110,H120:T120,H130:T130,H135:T135,H45:T45,H141:T141,H151:T151,H156:T156,H14:T14,H24:T24,H29:T29,H34:T34,H19:T19,H162:T162,H172:T172Expression=TIME(HOUR(H14),MINUTE(H14),0)+TIME(12,0,0)<TIME(HOUR(G15),MINUTE(G15),0)textNO
H40:T40,H50:T50,H55:T55,H60:T60,H65:T65,H70:T70,H75:T75,H80:T80,H85:T85,H90:T90,H95:T95,H100:T100,H105:T105,H110:T110,H120:T120,H130:T130,H135:T135,H45:T45,H141:T141,H151:T151,H156:T156,H14:T14,H24:T24,H29:T29,H34:T34,H19:T19,H162:T162,H172:T172Expression=AND(TIME(HOUR(G15),MINUTE(G15),0)<TIME(7,0,0),TIME(HOUR(G15),MINUTE(G15),0)+TIME(12,0,0)>TIME(HOUR(H14),MINUTE(H14),0))textNO
Cells with Data Validation
CellAllowCriteria
G38:T38List=DATA!$AF$3:$AF$42
G43:T43List=DATA!$AF$3:$AF$42
 
Upvote 0
Okay, so the values in rows 38 and 43 will change from EDO/EDO-U or OFF/OFF-U? Not looking at the values in rows 39-42 and 44-47 correct?
 
Upvote 0
Okay, so the values in rows 38 and 43 will change from EDO/EDO-U or OFF/OFF-U? Not looking at the values in rows 39-42 and 44-47 correct?
Hey mate, yeah that's correct. The rows below use a VLOOKUP to find the shift start and finish times as per Row 38 and 43 and will be reflected based on their value.
 
Upvote 0
Okay, see if this works. Copy all of it into the sheet module:
VBA Code:
Dim pVal

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
pVal = Target.Value
End Sub

Public Sub Worksheet_Change(ByVal Target As Range)
Dim prevValue
prevValue = pVal

If Not Intersect(Target, Range("G38:T38", "G43:T43")) Is Nothing Then
    If prevValue = "EDO" Or prevValue = "EDO-U" Then
        If InStr(Target.Value, "0") Then
            With Target
                .Interior.Color = RGB(0, 176, 240)
                .Font.Color = RGB(255, 0, 0)
            End With
        End If
    Else
        If prevValue = "OFF" Or prevValue = "OFF-U" Then
            If InStr(Target.Value, "0") Then
                With Target
                    .Interior.Color = RGB(255, 255, 0)
                    .Font.Color = RGB(255, 0, 0)
                End With
            End If
        End If
    End If
End If

End Sub
 
Upvote 0
Okay, see if this works. Copy all of it into the sheet module:
VBA Code:
Dim pVal

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
pVal = Target.Value
End Sub

Public Sub Worksheet_Change(ByVal Target As Range)
Dim prevValue
prevValue = pVal

If Not Intersect(Target, Range("G38:T38", "G43:T43")) Is Nothing Then
    If prevValue = "EDO" Or prevValue = "EDO-U" Then
        If InStr(Target.Value, "0") Then
            With Target
                .Interior.Color = RGB(0, 176, 240)
                .Font.Color = RGB(255, 0, 0)
            End With
        End If
    Else
        If prevValue = "OFF" Or prevValue = "OFF-U" Then
            If InStr(Target.Value, "0") Then
                With Target
                    .Interior.Color = RGB(255, 255, 0)
                    .Font.Color = RGB(255, 0, 0)
                End With
            End If
        End If
    End If
End If

End Sub
Hi mate thanks so much for taking the time to send this. I've tested it in another workbook and it works perfectly. 2 things I need some further help with if you don't mind?
- I rememebered it would be great to have an input box appear similar to the ones in the below code. It would pop up for the user to enter a comment that is then stacked on top of any other comment in the cell already.
- I already have some code on the sheet, where shall I fit it in?
The code is as follows.

Thanks so much for your ongoing help. Whilst I understand what excel can do, I am still a complete noob at the coding side, so it is very kind of you folk to help.
Cheers
Hayden

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Resp As String

If Target.CountLarge = 1 And Not Intersect(Target, Range("G10:T172")) Is Nothing Then
    With Target
      Select Case .Value
        Case "SDO", "STFN", "CDO", "CTFN" '<- Add more trigger values here if required
          Resp = Application.InputBox("Please insert details of absenteeism", _
          Title:="Absenteeism Details")
          If Len(Resp) > 0 And Resp <> "False" Then
            If Not .Comment Is Nothing Then
              .Comment.Text .Comment.Text & vbLf & Resp
            Else
              ActiveSheet.Unprotect
              .AddComment Text:=Resp
              ActiveSheet.Protect DrawingObjects:=False
            End If
          End If
      End Select
    End With
  End If

If Target.CountLarge = 1 And Not Intersect(Target, Range("G10:T172")) Is Nothing Then
    With Target
      Select Case .Value
        Case "B/OFF", "B/EDO" '<- Add more trigger values here if required
          Resp = Application.InputBox("Please insert details of DAO request to be marked unavailable on OFF/EDO.", _
          Title:="OFF/EDO Unavailability Details")
          If Len(Resp) > 0 And Resp <> "False" Then
            If Not .Comment Is Nothing Then
              .Comment.Text .Comment.Text & vbLf & Resp
            Else
              ActiveSheet.Unprotect
              .AddComment Text:=Resp
              ActiveSheet.Protect DrawingObjects:=False
            End If
          End If
      End Select
    End With
  End If
  
If Target.CountLarge = 1 And Not Intersect(Target, Range("G9:T172")) Is Nothing Then '<- Set relevant range in this line
    With Target
      If InStr(1, .Value, "?") > 0 Then
          Resp = Application.InputBox("Please enter details of when this shift extension was confirmed by the DAO. Including time and date and how it was confirmed", "DAO Shift Extension Confirmation", , , , , , 2)
          If Len(Resp) > 0 And Resp <> "False" Then
            If .Comment Is Nothing Then
              ActiveSheet.Unprotect
              .AddComment Text:=Resp
               ActiveSheet.Protect DrawingObjects:=False
            Else
              ActiveSheet.Unprotect
              .Comment.Text .Comment.Text & vbLf & Resp
              ActiveSheet.Protect DrawingObjects:=False
            End If
          End If
      End If
    End With
  End If
  
If Target.CountLarge = 1 And Not Intersect(Target, Range("G9:T172")) Is Nothing Then '<- Set relevant range in this line
    With Target
      If InStr(1, .Value, "OK") > 0 Then
          Resp = Application.InputBox("Please enter details of when this shift extension was confirmed by the DAO. Including time and date and how it was confirmed", "DAO Shift Extension Confirmation", , , , , , 2)
          If Len(Resp) > 0 And Resp <> "False" Then
            If .Comment Is Nothing Then
              ActiveSheet.Unprotect
              .AddComment Text:=Resp
              ActiveSheet.Protect DrawingObjects:=False
            Else
              ActiveSheet.Unprotect
              .Comment.Text .Comment.Text & vbLf & Resp
              ActiveSheet.Protect DrawingObjects:=False
            End If
          End If
      End If
    End With
  End If
  
If Target.CountLarge = 1 And Not Intersect(Target, Range("G9:T172")) Is Nothing Then '<- Set relevant range in this line
    With Target
      If InStr(1, .Value, "DEC") > 0 Then
          Resp = Application.InputBox("Please enter details of when this shift extension was declined by the DAO. Including time and date when it was declined", "DAO Shift Extension Declined", , , , , , 2)
          If Len(Resp) > 0 And Resp <> "False" Then
            If .Comment Is Nothing Then
              ActiveSheet.Unprotect
              .AddComment Text:=Resp
              ActiveSheet.Protect DrawingObjects:=False
            Else
              ActiveSheet.Unprotect
              .Comment.Text .Comment.Text & vbLf & Resp
              ActiveSheet.Protect DrawingObjects:=False
            End If
          End If
      End If
    End With
  End If
  

End Sub
 
Upvote 0
You have two different ranges, G10:T172 and G9:T172. Are those supposed to be different or should they both be G10:T172?

And what is full range for the cell/text color changes?
 
Upvote 0
Okay, I have cleaned up your code a bit and set the range to G9:T172 so if you need to adjust it, go for it. Also, I set the new input boxes to "Test Text" so you'll have to update those as well. Replace the code I gave you earlier and the code you just showed me with this:
VBA Code:
Dim pVal

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
pVal = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Resp As String
Dim prevValue
prevValue = pVal

On Error GoTo ExitNow
Application.EnableEvents = False
If Target.CountLarge = 1 And Not Intersect(Target, Range("G9:T172")) Is Nothing Then '<--- Change Target Range Here
'---EDO/EDO-U
    If prevValue = "EDO" Or prevValue = "EDO-U" Then
        If InStr(Target.Value, "0") Then
            With Target
                .Interior.Color = RGB(0, 176, 240)
                .Font.Color = RGB(255, 0, 0)
                'Input Box below. change text and title as needed:
                Resp = Application.InputBox("Test Text", _
                Title:="Test Text")
            End With
        End If
    Else
'-------OFF/OFF-U
        If prevValue = "OFF" Or prevValue = "OFF-U" Then
            If InStr(Target.Value, "0") Then
                With Target
                    .Interior.Color = RGB(255, 255, 0)
                    .Font.Color = RGB(255, 0, 0)
                    'Input Box below. change text and title as needed:
                    Resp = Application.InputBox("Test Text", _
                    Title:="Test text")
                End With
            End If
        End If
    End If
'---Absenteeism Details
    With Target
        Select Case .Value
            Case "SDO", "STFN", "CDO", "CTFN" '<- Add more trigger values here if required
                Resp = Application.InputBox("Please insert details of absenteeism", _
                Title:="Absenteeism Details")
            Case "B/OFF", "B/EDO" '<- Add more trigger values here if required
                Resp = Application.InputBox("Please insert details of DAO request to be marked unavailable on OFF/EDO.", _
                Title:="OFF/EDO Unavailability Details")
        End Select
    End With
'---DAO Shift Extension Confirmation/Decline
    With Target
        If InStr(1, .Value, "?") > 0 Or InStr(1, .Value, "OK") > 0 Then
            Resp = Application.InputBox("Please enter details of when this shift extension was confirmed by the DAO. " & _
            "Including time and date and how it was confirmed", "DAO Shift Extension Confirmation", , , , , , 2)
        Else
            If InStr(1, .Value, "DEC") > 0 Then
                Resp = Application.InputBox("Please enter details of when this shift extension was declined by the DAO. " & _
                "Including time and date when it was declined", "DAO Shift Extension Declined", , , , , , 2)
            End If
        End If
    End With
    Call AddComment(Target, Resp)
End If

ExitNow:
Application.EnableEvents = True
End Sub

Sub AddComment(rng As Range, cTxt As String)
With rng
    If Len(cTxt) > 0 And cTxt <> "False" Then
        If .Comment Is Nothing Then
            'ActiveSheet.Unprotect
            .AddComment Text:=cTxt
            'ActiveSheet.Protect DrawingObjects:=False
        Else
            'ActiveSheet.Unprotect
            .Comment.Text .Comment.Text & vbLf & cTxt
            'ActiveSheet.Protect DrawingObjects:=False
        End If
    End If
End With
End Sub
 
Upvote 0
Solution
You have two different ranges, G10:T172 and G9:T172. Are those supposed to be different or should they both be G10:T172?

And what is full range for the cell/text color changes?
Oh yes, good spotting. The range now after some changes is actually G12:T174, and will apply for this new code request as well.
Thanks mate
Hayden
 
Upvote 0

Forum statistics

Threads
1,222,581
Messages
6,166,887
Members
452,082
Latest member
pmajax

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