How to use VBA to do Conditional Formatting (NOT applying VBA)

EmmaG

New Member
Joined
Jan 9, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have two sheets.

Sheet 1 looks like this:

1578590087162.png


Sheet 2's layout is exactly like sheet1's, but rows' sequence may change and some $ amounts may change as well

1578590315636.png


Currently, I am using this VBA code to compare and contrast two sheets, then highlight the differences.

Sub Try()

Range("F2:F10").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(SUMIFS('Sheet1'!F:F, 'Sheet1'!A:A, A2, 'Sheet1'!D:D, D2)<>F2, A2<>"""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

End Sub

If I run the code above, it will find rows from both sheets with same PO/SO, same Activity, but different Amount, then highlight the Amount cells
1578590341304.png


However, this code is simply “applying” conditional formatting to the sheet. In other words, if I go to Conditional Formatting, and click “Manage Rules”, I will see the formula below in the Rule Manager box:

=AND(SUMIFS(Sheet1!F:F, Sheet1!A:A, A2, Sheet1!D:D, D2)<>F2, A2<>"")

What I really want is to use the code to “do” conditional formatting, not “apply” conditional formatting. I don’t want any rules exist in the Conditional Formatting Rules Manager box after I run the VBA.

I am fairly new to VBA, this is what I have for now:

Sub Test ()

Dim PrevPOSO As String

Dim PrevAct As String

Dim PrevAmount As String


For i = 2 To ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count

PrevPOSO = ThisWorkbook.Worksheets("Sheet1").Cells(i, 1).Value

PrevAct = ThisWorkbook.Worksheets("Sheet1").Cells(i, 4).Value

PrevAmount = ThisWorkbook.Worksheets("Sheet1").Cells(i, 6).Value



For s = 2 To ThisWorkbook.Worksheets("Sheet2").UsedRange.Rows.Count

If ThisWorkbook.Worksheets("Sheet2").Cells(s, 1).Value <> "" And ThisWorkbook.Worksheets("Sheet2").Cells(s, 1).Value = PrevPOSO And ThisWorkbook.Worksheets("Sheet2").Cells(s, 4).Value = PrevAct And ThisWorkbook.Worksheets("PCAM Commitments").Cells(s, 6).Value <> PrevAmount Then

ThisWorkbook.Worksheets("PCAM Commitments").Cells(s, 6).Interior.Color = 192

End If


Next s

Next i


End Sub

But if I try to run this code, nothing will happen...No error warning, no any reaction whatsoever. So any comments would be appreciated!
 

Attachments

  • 1578590153878.png
    1578590153878.png
    18.8 KB · Views: 8
  • 1578590214232.png
    1578590214232.png
    18.7 KB · Views: 8

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to MrExcel.
Will the PO/SO number only appear once on each sheet?
 
Upvote 0
Hi & welcome to MrExcel.
Will the PO/SO number only appear once on each sheet?
Hey Fluff!
No, PO/SO itself cannot serve as an unique identifier. PO/SO along with Activity is the unique identifier for each row in this case
 
Upvote 0
Ok, how about
VBA Code:
Sub EmmaG()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic.Item(Cl.Value & "|" & Cl.Offset(, 3).Value) = Cl.Offset(, 5).Value
      Next Cl
   End With
   With Sheets("Sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Cl.Value <> "" Then
            If Dic.Item(Cl.Value & "|" & Cl.Offset(, 3).Value) <> Cl.Offset(, 5).Value Then
               Cl.Offset(, 5).Interior.Color = 192
            End If
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub EmmaG()
   Dim Cl As Range
   Dim Dic As Object
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic.Item(Cl.Value & "|" & Cl.Offset(, 3).Value) = Cl.Offset(, 5).Value
      Next Cl
   End With
   With Sheets("Sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Cl.Value <> "" Then
            If Dic.Item(Cl.Value & "|" & Cl.Offset(, 3).Value) <> Cl.Offset(, 5).Value Then
               Cl.Offset(, 5).Interior.Color = 192
            End If
         End If
      Next Cl
   End With
End Sub
You are a life saver! It worked. Thank you!! I am not very familiar with the offset/create object functions in VBA, so I will do some research on your code
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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