Hello everybody,
I have code that compares the values in column D and G. When there is a difference between the two values, the value in column D is highlighted red. There are other criteria that count "acceptable" and will not highlight. These include if column G is N/A and Column D is 1 and if column G is N/A and column D is 0.
When I have been running some testing I have noticed two issues.
1. Whenever there is a 0.50 in column D, the code does not pick up that there is a difference, no matter what is in Column G on the same row.
2. Some of the highlights are put in column G but I just want column D highlighted when there is a difference.
Thank you for your consideration and thanks ahead of time to all that reply.
I have code that compares the values in column D and G. When there is a difference between the two values, the value in column D is highlighted red. There are other criteria that count "acceptable" and will not highlight. These include if column G is N/A and Column D is 1 and if column G is N/A and column D is 0.
When I have been running some testing I have noticed two issues.
1. Whenever there is a 0.50 in column D, the code does not pick up that there is a difference, no matter what is in Column G on the same row.
2. Some of the highlights are put in column G but I just want column D highlighted when there is a difference.
Code:
Option Explicit
Public Sub RateTest()
Dim ws As Worksheet, miss As Range, tmp As Range, t As Double
Dim max1 As Long, max2 As Long, colD As Range, colG As Range
t = Timer
Set ws = ThisWorkbook.Sheets("Basic Annual Premium")
max1 = ws.Cells(Rows.Count, "D").End(xlUp).Row
max2 = ws.Cells(Rows.Count, "G").End(xlUp).Row
Set colD = ws.Range(ws.Cells(2, "D"), ws.Cells(max1, "D"))
Set colG = ws.Range(ws.Cells(2, "G"), ws.Cells(max2, "G"))
colD.Interior.ColorIndex = xlColorIndexNone
colG.Interior.ColorIndex = xlColorIndexNone
Set miss = CheckColumns(colD, colG, "N/A")
If miss Is Nothing Then
Set miss = CheckColumns(colG, colD, "1")
Else
Set tmp = CheckColumns(colG, colD, "1")
If Not tmp Is Nothing Then Set miss = Union(miss, tmp)
End If
If Not miss Is Nothing Then miss.Interior.Color = RGB(255, 0, 0)
Debug.Print "Rows: " & max1 & "; Time: " & Format(Timer - t, "0.000") & " sec"
End Sub
Private Function CheckColumns(col1 As Range, col2 As Range, x As String) As Variant
Dim c As Variant, r As Long, d As Object, rng As Range
c = col1.Value2
Set d = CreateObject("Scripting.dictionary")
For r = 1 To UBound(c)
d(CStr(c(r, 1))) = vbNullString
Next
c = col2.Value2
For r = 1 To UBound(c)
If Len(c(r, 1)) > 0 Then
If c(r, 1) <> x Then
If Not d.exists(CStr(c(r, 1))) Then
If rng Is Nothing Then
Set rng = col2.Cells(r)
Else
Set rng = Union(rng, col2.Cells(r))
End If
End If
End If
End If
Next
Set CheckColumns = rng
End Function
Thank you for your consideration and thanks ahead of time to all that reply.