Sheet code for conditional formatting

MeaclH

Board Regular
Joined
Apr 2, 2014
Messages
77
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
 
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
Mate this is awesome. Have given it a quick test and all seems to be working perfectly.
Thank you heaps.
One thing I have noticed about your code is that it stacks the comments in chronological order, which is perfect.
I have another code below that I use that puts the newest comment on top. Do you have any suggestions to fix that?

Appreciate your help
Hayden

VBA Code:
Sub ConfirmShift()
    
    Dim sCmt As String
    Dim rCell As Range

    sCmt = InputBox( _
      Prompt:="Have you confirmed this extra shift with the DAO?" & vbCrLf & _
      "Please add your name, date and time this was confirmed. ", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "Shift remains unconfirmed. Please notify DAO or seek alternative."
        Exit Sub 'user cancelled inputbox / entered blank
    Else
        ActiveSheet.Unprotect
        For Each rCell In Selection
            With rCell
                If .Comment Is Nothing Then
                    .AddComment.Text sCmt
                Else
                    .Comment.Text sCmt & Chr(10) & .Comment.Text
                End If
            End With
        Next
        Set rCell = Nothing
        Selection.Interior.Color = RGB(215, 245, 215)
        ActiveSheet.Protect DrawingObjects:=False
    End If

End Sub
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I realized I left the comments on the unprotect and protect lines at the very bottom of the code in the "AddComment" sub, so make sure you uncomment those if you need the sheet protection.

And as for the comment order, I didn't change anything there in your original code, but try this for the other code:
VBA Code:
For Each rCell In Selection
            With rCell
                If .Comment Is Nothing Then
                    .AddComment.Text sCmt
                Else
                    .Comment.Text & Chr(10) & .Comment.Text sCmt
                End If
            End With
        Next
 
Upvote 0
I realized I left the comments on the unprotect and protect lines at the very bottom of the code in the "AddComment" sub, so make sure you uncomment those if you need the sheet protection.

And as for the comment order, I didn't change anything there in your original code, but try this for the other code:
VBA Code:
For Each rCell In Selection
            With rCell
                If .Comment Is Nothing Then
                    .AddComment.Text sCmt
                Else
[B]                    .Comment.Text & Chr(10) & .Comment.Text sCmt[/B]
                End If
            End With
        Next
Hi mate, thanks again. I get a compile error: syntax error when I changed the above. Ideas?
Cheers
Hayden
 
Upvote 0
Hi mate, thanks again. I get a compile error: syntax error when I changed the above. Ideas?
Cheers
Hayden
Not sure, maybe this for the Else section:

VBA Code:
.Comment.Text .Comment.Text & vbLf & sCmt
 
Upvote 0
Great! You're welcome, and happy to help.
Mate, you're going to hate me. And totally understand if you've had enough of this one.
I run another macro, the one below. It works great and was provided by one of the friendly folk on here.
The only problem is now when I run it, it also triggers the new code you have helped with when EDO/EDO-U and OFF/OFF-U are changed.

Is there a way around this? So that when the below macro is run it doesn't trigger the change of cell color and another input box.

Cheers mate
Hayden
VBA Code:
Sub ActionSwap()
    Dim sCmt As String
    Dim i As Long
    Dim rCell As Range
    Dim area1 As Variant, area2 As Variant, swapval As Variant
    
    sCmt = InputBox( _
      Prompt:="Enter details of the swap. Including when it was actioned and by who." & vbCrLf & _
      "Comment will be added to all cells in Selection", _
      Title:="DAO Swap Details")
    If sCmt = "" Then
        MsgBox "No comment added"
    Else
        For Each rCell In Selection
        With rCell
            If .Comment Is Nothing Then
            .AddComment.Text sCmt
        Else
            .Comment.Text sCmt & vbLf & .Comment.Text
        End If
    End With
Next
    End If
    Set rCell = Nothing
    If Selection.Areas.Count <> 2 Then Exit Sub
    If Selection.Areas(1).Columns.Count <> Selection.Areas(2).Columns.Count Then
        MsgBox ("Selection areas must have the same number of columns")
        Exit Sub
    End If
    area1 = Selection.Areas(1)
    area2 = Selection.Areas(2)
    If Selection.Areas(1).Columns.Count = 1 Then
        swapval = area1
        area1 = area2
        area2 = swapval
    Else
        For i = LBound(area1, 2) To UBound(area1, 2)
            swapval = area1(1, i)
            area1(1, i) = area2(1, i)
            area2(1, i) = swapval
        Next
    End If
    Selection.Areas(1) = area1
    Selection.Areas(2) = area2
    
End Sub
 
Upvote 0
Mate, you're going to hate me. And totally understand if you've had enough of this one.
I run another macro, the one below. It works great and was provided by one of the friendly folk on here.
The only problem is now when I run it, it also triggers the new code you have helped with when EDO/EDO-U and OFF/OFF-U are changed.

Is there a way around this? So that when the below macro is run it doesn't trigger the change of cell color and another input box.

Cheers mate
Hayden
VBA Code:
Sub ActionSwap()
    Dim sCmt As String
    Dim i As Long
    Dim rCell As Range
    Dim area1 As Variant, area2 As Variant, swapval As Variant
  
    sCmt = InputBox( _
      Prompt:="Enter details of the swap. Including when it was actioned and by who." & vbCrLf & _
      "Comment will be added to all cells in Selection", _
      Title:="DAO Swap Details")
    If sCmt = "" Then
        MsgBox "No comment added"
    Else
        For Each rCell In Selection
        With rCell
            If .Comment Is Nothing Then
            .AddComment.Text sCmt
        Else
            .Comment.Text sCmt & vbLf & .Comment.Text
        End If
    End With
Next
    End If
    Set rCell = Nothing
    If Selection.Areas.Count <> 2 Then Exit Sub
    If Selection.Areas(1).Columns.Count <> Selection.Areas(2).Columns.Count Then
        MsgBox ("Selection areas must have the same number of columns")
        Exit Sub
    End If
    area1 = Selection.Areas(1)
    area2 = Selection.Areas(2)
    If Selection.Areas(1).Columns.Count = 1 Then
        swapval = area1
        area1 = area2
        area2 = swapval
    Else
        For i = LBound(area1, 2) To UBound(area1, 2)
            swapval = area1(1, i)
            area1(1, i) = area2(1, i)
            area2(1, i) = swapval
        Next
    End If
    Selection.Areas(1) = area1
    Selection.Areas(2) = area2
  
End Sub
Perhaps some sort of enableevents change? Not sure how to implement it though....
 
Upvote 0
I added another global var to set as a flag between the two subs, so that should help:
VBA Code:
Dim pVal
Dim swapVal As Boolean

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("G12:T174")) Is Nothing Then '<--- Change Target Range Here
    If swapVal = False Then
'---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
    Else
        swapVal = False
    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

Sub ActionSwap()
Dim sCmt As String
Dim i As Long
Dim rCell As Range
Dim area1 As Variant, area2 As Variant, swapVal As Variant
    
sCmt = InputBox( _
  Prompt:="Enter details of the swap. Including when it was actioned and by who." & vbCrLf & _
  "Comment will be added to all cells in Selection", _
  Title:="DAO Swap Details")
If sCmt = "" Then
    MsgBox "No comment added"
Else
    For Each rCell In Selection
    With rCell
        If .Comment Is Nothing Then
        .AddComment.Text sCmt
    Else
        .Comment.Text sCmt & vbLf & .Comment.Text
    End If
End With
Next
End If
Set rCell = Nothing
If Selection.Areas.Count <> 2 Then Exit Sub
If Selection.Areas(1).Columns.Count <> Selection.Areas(2).Columns.Count Then
    MsgBox ("Selection areas must have the same number of columns")
    Exit Sub
End If
area1 = Selection.Areas(1)
area2 = Selection.Areas(2)
If Selection.Areas(1).Columns.Count = 1 Then
    swapVal = area1
    area1 = area2
    area2 = swapVal
Else
    For i = LBound(area1, 2) To UBound(area1, 2)
        swapVal = area1(1, i)
        area1(1, i) = area2(1, i)
        area2(1, i) = swapVal
    Next
End If
Selection.Areas(1) = area1
Selection.Areas(2) = area2
swapVal = True
End Sub
 
Upvote 0
I added another global var to set as a flag between the two subs, so that should help:
VBA Code:
Dim pVal
Dim swapVal As Boolean

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("G12:T174")) Is Nothing Then '<--- Change Target Range Here
    If swapVal = False Then
'---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
    Else
        swapVal = False
    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

Sub ActionSwap()
Dim sCmt As String
Dim i As Long
Dim rCell As Range
Dim area1 As Variant, area2 As Variant, swapVal As Variant
   
sCmt = InputBox( _
  Prompt:="Enter details of the swap. Including when it was actioned and by who." & vbCrLf & _
  "Comment will be added to all cells in Selection", _
  Title:="DAO Swap Details")
If sCmt = "" Then
    MsgBox "No comment added"
Else
    For Each rCell In Selection
    With rCell
        If .Comment Is Nothing Then
        .AddComment.Text sCmt
    Else
        .Comment.Text sCmt & vbLf & .Comment.Text
    End If
End With
Next
End If
Set rCell = Nothing
If Selection.Areas.Count <> 2 Then Exit Sub
If Selection.Areas(1).Columns.Count <> Selection.Areas(2).Columns.Count Then
    MsgBox ("Selection areas must have the same number of columns")
    Exit Sub
End If
area1 = Selection.Areas(1)
area2 = Selection.Areas(2)
If Selection.Areas(1).Columns.Count = 1 Then
    swapVal = area1
    area1 = area2
    area2 = swapVal
Else
    For i = LBound(area1, 2) To UBound(area1, 2)
        swapVal = area1(1, i)
        area1(1, i) = area2(1, i)
        area2(1, i) = swapVal
    Next
End If
Selection.Areas(1) = area1
Selection.Areas(2) = area2
swapVal = True
End Sub
Mate once again. THANK YOU. You are going to make me look very very good! Appreciate your ongoing help.
Cheers,
Hayden
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

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