# Conditional Formatting with Excel - Part 3

Hello experts,

I have conditional format in column L that highlights the cell when the value = value in cell D2. I would like to tweak this conditon to highlight when the cell in D2 is L2 - 0.02. So, in the example D2 is currently 37.24 and L2 is 37.28. I would like the cell to highlight when the value in D2 reaches 37.26.

###### Who is Participating?

Commented:
On the other thought.......are you calling this function? You probably need the line

Call CheckD

after this code
If Not Point02 Then Cells(1, 12).Interior.Color = 65535
Cells(1, 12).Interior.Color = 65535

Anyway I attached the code, and I also added the line "call cehckD" as mentioned above.
EE.xls
0

Commented:
Do you mean exactly equal to L2-0.02 or greater than or equal to that?
0

Commented:
Use this for conditional formatting for cell D2

=ROUND(\$L\$2,2)-0.02
0

Author Commented:
hitsdoshi1

That worked.

Would you know how to attach somekind of sound alert to that. At the moment, I have the following alert on column L:

For Each xCell In Range("l2:l" & xLastRow)
xValue = xCell.Value
xValue2 = xCell.Offset(0, -8).Value
If Not IsError(xValue) And Not IsError(xValue2) Then
If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then
PlayTheSound "Windows XP Print complete.wav"

Cells(1, 12).Interior.Color = 255
Exit Sub
End If
End If
Next

Cells(1, 12).Interior.Color = 65535

Do you think I could use that for column D?

Cheers mate....
0

Commented:
I will go with something simple like this...

For ctr = 2 To 65000
DVal = Round(Range("D" & ctr).Value, 2)
Lval = (Round(Range("L" & ctr).Value, 2)) - 0.02

If Range("D" & ctr) = "" Then Exit For

If DVal = Lval Then
PlayTheSound "Windows XP Print complete.wav"
End If

Next
0

Author Commented:
hitsdoshi1

I got a compile error with:

For ctr

0

Author Commented:
Its says 'variable not defined'
0

Commented:
Try this one...

Sub CheckD()
Dim ctr As Long
For ctr = 2 To 65000
If Range("D" & ctr) = "" Then Exit For
If Range("D" & ctr) = Round(Range("L" & ctr).Value, 2) - 0.02 Then
PlayTheSound "Windows XP Print complete.wav"
End If
Next
End Sub
0

Commented:
You can write exit sub instead of exit for....

Sub CheckD()
Dim ctr As Long
For ctr = 2 To 65000
If Range("D" & ctr) = "" Then Exit Sub
If Range("D" & ctr) = Round(Range("L" & ctr).Value, 2) - 0.02 Then
PlayTheSound "Windows XP Print complete.wav"
End If
Next
End Sub
0

Author Commented:
hitsdoshi1

Thanks again for responding.

I keep on getting the error message 'Expected End Sub'. When I put in an end sub it keeps on giving me the same error message

0

Commented:

Sub CheckD()
Dim ctr As Long
For ctr = 2 To 65000
If Range("D" & ctr) = "" Then
Exit Sub
Endif
If Range("D" & ctr) = Round(Range("L" & ctr).Value, 2) - 0.02 Then
PlayTheSound "Windows XP Print complete.wav"
End If
Next
End Sub
0

Author Commented:
hitsdoshi1

As you can see from the attached sample, I've placed the code you suggested in but I keep on getting the error when I changed the value from 10.00 in cell D2 to 9.98
0

Commented:
Its cos you are inserting the sub CheckD() in another Sub Worksheet_Calculate(), and that's what causes the error...

This is what you have...

Private Sub Worksheet_Calculate()
Dim xCell As Range
Dim xLastRow As Long
Dim xValue As Variant
Dim xValue2 As Variant
Dim Point02 As Boolean

xLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If xLastRow < 2 Then Exit Sub

For Each xCell In Range("O2:O" & xLastRow)
xValue = xCell.Value
If Not IsError(xValue) Then
If xValue <> "" And xValue = 0.0002 Then
Cells(1, 15).Interior.Color = 255
Point02 = True
Exit For
End If
End If
Next

If Not Point02 Then Cells(1, 15).Interior.Color = 65535

For Each xCell In Range("q2:q" & xLastRow)
xValue = xCell.Value
xValue2 = xCell.Offset(0, -13).Value
If Not IsError(xValue) And Not IsError(xValue2) Then
If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then

Cells(1, 17).Interior.Color = 255
Exit Sub
End If
End If
Next

Cells(1, 17).Interior.Color = 65535
If Not Point02 Then Cells(1, 12).Interior.Color = 65535

Sub CheckD()
Dim ctr As Long
For ctr = 2 To 65000
If Range("D" & ctr) = "" Then Exit Sub
If Range("D" & ctr) = Round(Range("L" & ctr).Value, 2) - 0.02 Then
PlayTheSound "Windows XP Print complete.wav"
End If
Next
End Sub

Cells(1, 12).Interior.Color = 65535

End Sub

This is what you need.....just remove the lines 9 to 65 and paste the following code...

``````Private Sub Worksheet_Calculate()
Dim xCell As Range
Dim xLastRow As Long
Dim xValue As Variant
Dim xValue2 As Variant
Dim Point02 As Boolean

xLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If xLastRow < 2 Then Exit Sub

For Each xCell In Range("O2:O" & xLastRow)
xValue = xCell.Value
If Not IsError(xValue) Then
If xValue <> "" And xValue = 0.0002 Then
Cells(1, 15).Interior.Color = 255
Point02 = True
Exit For
End If
End If
Next

If Not Point02 Then Cells(1, 15).Interior.Color = 65535

For Each xCell In Range("q2:q" & xLastRow)
xValue = xCell.Value
xValue2 = xCell.Offset(0, -13).Value
If Not IsError(xValue) And Not IsError(xValue2) Then
If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then

Cells(1, 17).Interior.Color = 255
Exit Sub
End If
End If
Next

Cells(1, 17).Interior.Color = 65535
If Not Point02 Then Cells(1, 12).Interior.Color = 65535
Cells(1, 12).Interior.Color = 65535

End Sub

Sub CheckD()
Dim ctr As Long
For ctr = 2 To 65000
If Range("D" & ctr) = "" Then Exit Sub
If Range("D" & ctr) = Round(Range("L" & ctr).Value, 2) - 0.02 Then
PlayTheSound "Windows XP Print complete.wav"
End If
Next
End Sub
``````
0

Author Commented:
hitsdoshi1

I think I've managed to get it working without any errors, however there isn't any sound when I change the number.....
0

Commented:
it only plays sound if D# is equal to L#-0.02

I just checked with your file, where D2=10 and L2=10, so I changed the value of D2 to 9.98....so in the code now it matches D2=L2-0.02......and it plays the sound...
0

Author Commented:
hitsdoshi1

Thats great, still having a problem with the sound....
0

Commented:
why don't you forward me your code again and let me check what changes you have made...
0

Author Commented:
hitsdoshi

Thanks, here you go....

Option Explicit
Private Declare Function sndPlaySound32 _
Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Private Sub Worksheet_Calculate()
Dim xCell As Range
Dim xLastRow As Long
Dim xValue As Variant
Dim xValue2 As Variant
Dim Point02 As Boolean

xLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If xLastRow < 2 Then Exit Sub

For Each xCell In Range("O2:O" & xLastRow)
xValue = xCell.Value
If Not IsError(xValue) Then
If xValue <> "" And xValue = 0.0002 Then
Cells(1, 15).Interior.Color = 255
Point02 = True
Exit For
End If
End If
Next

If Not Point02 Then Cells(1, 15).Interior.Color = 65535

For Each xCell In Range("q2:q" & xLastRow)
xValue = xCell.Value
xValue2 = xCell.Offset(0, -13).Value
If Not IsError(xValue) And Not IsError(xValue2) Then
If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then

Cells(1, 17).Interior.Color = 255
Exit Sub
End If
End If
Next

Cells(1, 17).Interior.Color = 65535
If Not Point02 Then Cells(1, 12).Interior.Color = 65535
Cells(1, 12).Interior.Color = 65535

End Sub

Sub CheckD()
Dim ctr As Long
For ctr = 2 To 65000
If Range("D" & ctr) = "" Then Exit Sub
If Range("D" & ctr) = Round(Range("L" & ctr).Value, 2) - 0.02 Then
PlayTheSound "Windows XP Print complete.wav"
End If
Next
End Sub

Sub PlayTheSound(ByVal WhatSound As String)
If Dir(WhatSound, vbNormal) = "" Then
' WhatSound is not a file. Get the file named by
' WhatSound from the Windows\Media directory.
WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
If InStr(1, WhatSound, ".") = 0 Then
' if WhatSound does not have a .wav extension,
WhatSound = WhatSound & ".wav"
End If
If Dir(WhatSound, vbNormal) = vbNullString Then
' Can't find the file. Do a simple Beep.
Beep
Exit Sub
End If
Else
' WhatSound is a file. Use it.
End If
' Finally, play the sound.
sndPlaySound32 WhatSound, 0&
End Sub

0

Commented:
ok, just check the code line by line and its perfect. Works for me....the potential problem I can see is, if you are missing the sound file....just to make sure...do the following....

Insert this line....at the end right before End Sub and After sndPlaySound32

MsgBox "This works!!!"

and match one of the cell....for ex......make D2=L2-0.02.....so as per your earlier attachment, I had D2 as 0.98 and L2 was 10-0.02 and it matched and played the sound....so if it matches then it will play the sound and should pop up message window. If msg window pops up then code is fine, you might want to check the path of your sound file.
0

Author Commented:
hitsdoshi1,

You wrote D2 "0.98" and L2 "10-0.02". Did you mean D2 9.98 and L2 10.00?

0

Author Commented:
hitsdoshi1,

What might be a good idea is if you update the attachment with the code yourself and post it back.

0

Author Commented:
hitsdoshi1

Its amazing how its working for you. I don't even get the MsgBox "This Works"

0

Author Commented:
Ahhhhhh,

It seems to work when you click on 'Run/UserForm and the cursor is under Sub CheckD()

But it won't run automatically.....
0

Commented:
ok, follow the instruction in my last comment and it will run automatically
0

Author Commented:

hitsdoshi1,

Nearly there, I'm getting a Type Mismatch with:

If Range("D" & ctr) = "" Then
0

Author Commented:
hitsdoshi1,

My mistake, its working fine.....

Thanks man.....

Really, really appreciate it...
0

Commented:

If (IsError(Range("D" & ctr))) Then Exit Sub

before

if Range("D" & ctr)="" Then..
0

Author Commented:
hitsdoshi

As everything is working fine, should I still add

If (IsError(Range("D" & ctr))) Then Exit Sub

0

Commented:
It just checks, like if there is division error or something, code will give you error message, so it doesn't harm in adding this line...but it will terminate the code if it encounters error
0

Author Commented:
ok, thanks again mate..
0

Author Commented:
Excellent!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.