IF and AND statements

S

Sam Harman

My original thred has gone cold so thought i would post an update..

thanks in advance

Sam

------------------------------------



Yes sorry I appreciate that it is difficult

What I can tell you is this is an example from two lines of the table

R DP DQ DR = DP-DQ) DS DT = (DP-R)
0 79 80 -1 -1 79
3 74 76 -2 -2 71

Ideally I would like to conditionally format DT line two as the value
of this line (71) is less than DP (74). The first line would not be
formatted as the value of DTand DP is the same. Also if any cell in DP
has a value of 0 I want to ignore that completely regardless of what
value is in cell DT

Hope that helps


Regards

Sam
 
R

Ron Rosenfeld

My original thred has gone cold so thought i would post an update..

thanks in advance

Sam

------------------------------------



Yes sorry I appreciate that it is difficult

What I can tell you is this is an example from two lines of the table

R DP DQ DR = DP-DQ) DS DT = (DP-R)
0 79 80 -1 -1 79
3 74 76 -2 -2 71

Ideally I would like to conditionally format DT line two as the value
of this line (71) is less than DP (74). The first line would not be
formatted as the value of DTand DP is the same. Also if any cell in DP
has a value of 0 I want to ignore that completely regardless of what
value is in cell DT

Hope that helps


Regards

Sam

The conditional format formula for DT1 is:

=AND($DT1<$DP1,$DP1<>0)

You can either select DT1 / Conditional Formatting / Formula Is: to enter and format; then use the format painter to copy the formatting to other cells

or you can select the range of cells first (to include DT1).
 
S

Sam Harman

Hi Ron, I hope you are well and thanks for such a swift resolution to
my query.

However, I have a problem on my hands which is disastrous and
hopefully you will be able to assist.

You may remember that you kindly provided me with code for CountYellow
and CountFmt (Format) which would allow me to count the number of
cells which were coloured yellow or had conditional formatting.

These have been working flawlessley for weeks. However, today when I
opened my spreadsheet and tried to calculate some cells I got an error
message as follows:

Conditional formatting cannot be processed.

When I broke the code and went into the debugger it had highlighted
the CountFmt module and in yellow the Exit Function was highlighted.

I really hope you can help identify the issue here as these modules
are a key part of my spreadsheet.

With kind regards

Sam

----------------------------------------

Below is the CountFmr module code you created for me.


Function CountFmt(rg As Range) As Long
Dim c As Range
Dim t As Long
Dim fc As FormatCondition
Dim c1 As Double, c2 As Double
For Each c In rg
If c.Interior.ColorIndex <> xlColorIndexNone Then
t = t + 1
ElseIf c.FormatConditions.Count > 0 Then
For Each fc In c.FormatConditions
Select Case fc.Type
Case Is = xlCellValue
If fc.Operator <> xlBetween Then
MsgBox ("Conditional Format cannot be processed")
Exit Function
End If
c1 = Evaluate(fc.Formula1)
c2 = Evaluate(fc.Formula2)
If c.Value >= c1 And c.Value <= c2 Then
t = t + 1
End If
Case Is = xlExpression
If Evaluate(fc.Formula1) = True Then t = t + 1
Case Else
MsgBox ("Conditional Format cannot be processed")
Exit Function
End Select
Next fc
End If
Next c
CountFmt = t
End Function
 
R

Ron Rosenfeld

However, I have a problem on my hands which is disastrous and
hopefully you will be able to assist.

You may remember that you kindly provided me with code for CountYellow
and CountFmt (Format) which would allow me to count the number of
cells which were coloured yellow or had conditional formatting.

These have been working flawlessley for weeks. However, today when I
opened my spreadsheet and tried to calculate some cells I got an error
message as follows:

Conditional formatting cannot be processed.

When I broke the code and went into the debugger it had highlighted
the CountFmt module and in yellow the Exit Function was highlighted.

There are several error messages and Exit Function lines in that code.

But here is probably the key point, from my post with the code:

"If, indeed, your conditional formatting is limited to:

Either an expression, as in your cell A1, or various cell values, as in some of the other cells
AND IF
where there are several possible CF conditions, only ONE might be applicable (i.e. the conditions do not overlap),
then the following UDF should work: "

So most likely the code is hitting a type of conditional formatting that is not included in that description.

Note there are TWO message boxes with that error.

I think if you change the message, in both messageboxes, to:

MsgBox ("Conditional Format cannot be processed in cell: " & c.Address _
& vbLf & "Type: " & fc.Type _
& vbLf & "Operator: " & fc.Operator _
& vbLf & "Formula1: " & fc.Formula1)

it should provide you with some information as to which cell has the conditional formatting that is causing the problem, and some information about the formatting. If the function starts to return a #VALUE! error, then try just:

MsgBox ("Conditional Format cannot be processed in cell: " & c.Address _
& vbLf & "Type: " & fc.Type _
& vbLf & "Operator: " & fc.Operator)
 
S

Sam Harman

Hi Ron, and as usual thank you so much for your reply.

You have no idea how worried I was that all my work (and all of your
formulae etc !) was going to be undone by this error which I could not
track down...your code helped me identify the problem which was to do
with the conditional formatting of cells which I hope I have managed
to rectify and so far so good.

I would just like to add Ron, that people on these forums and for me,
you in particular typify the benefits of the internet. Without your
help my spreadsheet would be nowhere near as polished as it is and you
have saved me so much time with your help, advice and in particular
the modules which have all worked brilliantly. I am quite astounded by
your knowledge of excel but more so in the time you have given me to
pass on the benefit of your expertise.

I wish you all the best for the holidays and I will be back soon to
test your patience again after xmas and into the new year :)

Hope you don't mind !

Kind regards

Sam
 
R

Ron Rosenfeld

Hi Ron, and as usual thank you so much for your reply.

You have no idea how worried I was that all my work (and all of your
formulae etc !) was going to be undone by this error which I could not
track down...your code helped me identify the problem which was to do
with the conditional formatting of cells which I hope I have managed
to rectify and so far so good.

I would just like to add Ron, that people on these forums and for me,
you in particular typify the benefits of the internet. Without your
help my spreadsheet would be nowhere near as polished as it is and you
have saved me so much time with your help, advice and in particular
the modules which have all worked brilliantly. I am quite astounded by
your knowledge of excel but more so in the time you have given me to
pass on the benefit of your expertise.

I wish you all the best for the holidays and I will be back soon to
test your patience again after xmas and into the new year :)

Hope you don't mind !

Kind regards

Sam

Glad to have helped, and thank you for those kind words.
By the way, if it is necessary for you to use the type of CF that was causing the problem, we might be able to modify the macro to take it into account.

And I wish you and yours all the best for the holidays, also.
 
S

Sam Harman

Hi Ron, hope you had a good xmas.

I have encountered a problem since correcting the CF of the cells that
were causing an error in that the modules which you cretaed to colour
the cells based on their number are misbehaving !

For exmaple....the following macro which has flawlessly highlighted
the lowest number as yellow background / red font and the next three
loest numbers as green background / red font....is now not working
properly. No other data has been changed, no formats etc and the cells
which the macro looks in are all as they were before I had the problem
with the 'Cannot process CF cells error messge...

I know it must be difficult for you to work out what might have gone
wrong but I am guessing itmust be linked to the errors I was getting
previously.

The Color4New and other variances I have added to the spreadsheet
(Color3New etc) are all fundamental aspects of it operating properly
so I am at a loss to understand why this has happened.

Is there anything in the code below that might help point me in the
right direction?

Thanks in advance

Sam

-------------------------------------------------------------

Sub Color4New2011()
Dim rTimes As Range, rValues As Range, c As Range
Const NumToColor As Long = 4
Dim APOffset() As Long
Dim tTimes() As Variant, dPVals() As Double
Dim collTime As Collection, collColQ As Collection
Dim bLowest As Boolean
Dim i As Long, j As Long, k As Long

On Error Resume Next

Set rTimes = Application.InputBox(Prompt:="Select the Times", _
Default:=Selection.Address, Type:=8)
If rTimes Is Nothing Then Exit Sub

Set rValues = Application.InputBox("Select One (1) cell in each column
of Values ", Type:=8)
If rValues Is Nothing Then Exit Sub

On Error GoTo 0

bLowest = IIf(MsgBox("Lowest " & NumToColor & "?", vbYesNo) = vbYes,
True, False)

ReDim APOffset(0 To rValues.Count - 1)
i = 0
For Each c In rValues
APOffset(i) = c.Column - rTimes.Column
i = i + 1
Next c

'Unique list of times
Set collTime = New Collection
On Error Resume Next
For Each c In rTimes
collTime.Add Item:=c.Value, Key:=CStr(c.Value)
Next c
On Error GoTo 0

ReDim tTimes(0 To collTime.Count - 1, 0 To 2)
For i = 0 To collTime.Count - 1
tTimes(i, 0) = collTime(i + 1)
Next i

Application.ScreenUpdating = False
For k = 0 To UBound(APOffset)
'unique list of rValues values for each time
For i = 0 To UBound(tTimes, 1)
Set collColQ = New Collection
On Error Resume Next
For Each c In rTimes
If c.Value = tTimes(i, 0) Then
With c.Offset(columnoffset:=APOffset(k))
If bLowest = True Then collColQ.Add Item:=.Value,
Key:=CStr(.text)
If bLowest = False And .Value <> 0 Then
collColQ.Add Item:=.Value, Key:=CStr(.text)
End With
End If
Next c
On Error GoTo 0
If collColQ.Count > 0 Then
ReDim dPVals(0 To collColQ.Count - 1)
For j = 0 To UBound(dPVals)
dPVals(j) = collColQ(j + 1)
Next j
End If
With WorksheetFunction
If bLowest Then
tTimes(i, 1) = .Small(dPVals, .Min(UBound(dPVals) + 1,
NumToColor))
tTimes(i, 2) = .Min(dPVals)
Else
tTimes(i, 1) = .Large(dPVals, .Min(UBound(dPVals) + 1,
NumToColor))
tTimes(i, 2) = .Max(dPVals)
End If

End With
Next i

'color the cells
For i = 0 To UBound(tTimes, 1)
For Each c In rTimes
If c.Value = tTimes(i, 0) Then
With c.Offset(columnoffset:=APOffset(k))
If bLowest = False Then
'Select Case CDbl(.Text)
Select Case .Value
Case Is = tTimes(i, 2)
.Interior.Color = vbYellow
.Font.Color = vbRed
Case Is >= tTimes(i, 1)
.Interior.Color = vbGreen
.Font.Color = vbRed
Case Else
.Interior.Color = xlNone
.Font.Color = vbBlack
End Select
ElseIf bLowest = True Then
'Select Case CDbl(.Text)
Select Case .Value
Case Is = ""
.Interior.Color = xlNone
.Font.Color = vbBlack
Case Is = tTimes(i, 2)
.Interior.Color = vbYellow
.Font.Color = vbRed
Case Is <= tTimes(i, 1)
.Interior.Color = vbGreen
.Font.Color = vbRed
Case Else
.Interior.Color = xlNone
.Font.Color = vbBlack
End Select
End If
End With
End If
Next c
Next i
Next k
Application.ScreenUpdating = True
End Sub
 
R

Ron Rosenfeld

Hi Ron, hope you had a good xmas.

I have encountered a problem since correcting the CF of the cells that
were causing an error in that the modules which you cretaed to colour
the cells based on their number are misbehaving !

For exmaple....the following macro which has flawlessly highlighted
the lowest number as yellow background / red font and the next three
loest numbers as green background / red font....is now not working
properly. No other data has been changed, no formats etc and the cells
which the macro looks in are all as they were before I had the problem
with the 'Cannot process CF cells error messge...

I know it must be difficult for you to work out what might have gone
wrong but I am guessing itmust be linked to the errors I was getting
previously.

The Color4New and other variances I have added to the spreadsheet
(Color3New etc) are all fundamental aspects of it operating properly
so I am at a loss to understand why this has happened.

Is there anything in the code below that might help point me in the
right direction?

Thanks in advance

Sam

Sam,

I am going out of town in an hour, and do not have time to look into this now. If you could post or email me a copy of the errant workbook, with a clear explanation of what you mean by "not working properly", it would simplify the troubleshooting process when I return. To email me, use "mo c TO D e ni l no d le f ne s or T An o r " , remove the spaces, reverse it and make the obvious substitutions.
 
S

Sam Harman

Thanks Ron - have sent :)

Sam

Sam,

I am going out of town in an hour, and do not have time to look into this now. If you could post or email me a copy of the errant workbook, with a clear explanation of what you mean by "not working properly", it would simplify the troubleshooting process when I return. To email me, use "mo c TO D e ni l no d le f ne s or T An o r " , remove the spaces, reverse it and make the obvious substitutions.
 
R

Ron Rosenfeld

Hi Ron, hope you had a good xmas.

I have encountered a problem since correcting the CF of the cells that
were causing an error in that the modules which you cretaed to colour
the cells based on their number are misbehaving !

For exmaple....the following macro which has flawlessly highlighted
the lowest number as yellow background / red font and the next three
loest numbers as green background / red font....is now not working
properly. No other data has been changed, no formats etc and the cells
which the macro looks in are all as they were before I had the problem
with the 'Cannot process CF cells error messge...

I know it must be difficult for you to work out what might have gone
wrong but I am guessing itmust be linked to the errors I was getting
previously.

The Color4New and other variances I have added to the spreadsheet
(Color3New etc) are all fundamental aspects of it operating properly
so I am at a loss to understand why this has happened.

Is there anything in the code below that might help point me in the
right direction?

Thanks in advance

Sam

OK, Sam. I have had a chance to look at the workbook you posted on SkyDrive.

Looking at that workbook you provided, I don't see the problem. It certainly seems to me as if the lowest four entries for each time are being highlighted, with the special highlighting for the lowest. It is harder to see because of the way you have it sorted, but if you scroll down (or sort just by times), it seems as if the macro is working as designed.

Ron
 
R

Ron Rosenfeld

OK, Sam. I have had a chance to look at the workbook you posted on SkyDrive.

Looking at that workbook you provided, I don't see the problem. It certainly seems to me as if the lowest four entries for each time are being highlighted, with the special highlighting for the lowest. It is harder to see because of the way you have it sorted, but if you scroll down (or sort just by times), it seems as if the macro is working as designed.

Ron

Posting a followup here for this thread for completeness. Copies have already gone to Sam:
Having read your email I have now figured out what has gone wrong and it is not the macro as you rightly say, but it is a condition that wasnt known at the time the original macro was created.
What has happened is that this particular day is a rare ocassion when more than one race goes off at the same time..e.g the 13:40 at Ffos Las and the 13:40 Wincanton. Because this condition wasnt known to happen the macro rightly looks at the 1.40 combines all FC odds for that time span and picks the top 4 correctly.
Is it possible to amend the macro so that it looks at the time column and the course column before it selects the top four values in the FC odds column as this would ensure that this cannot happen? If it is a time consuming change please don't bother as I will find a way to work around it. I am just glad that with your help I have worked out what was happening !!

Being basically lazy, instead of rewriting the macro, I wrote a new macro (2 of them, actually).
They depend on several assumtions:
· Your column labels will always be in Row 1
· The "Time" column will always be labeled "Time"
· The "Course" column will always be labeled "Course"
· You will never use fractional seconds in determining the start time of the race.

Algorithm: Devise a list of the course names. For each course, add a defined amount of time (in 1/1000 of a second - limit of resolution) to the time so that each course, even though starting at the same time, will be different by 1/1000 of a second).
Then, when you run the Color4New2011 macro, it will differentiate the different race tracks.
When done, reset the times to remove the fractional seconds so nothing else gets affected.

=====================================
Option Explicit
Sub IncreaseTimes()
'indexes times to differentiate Courses
Dim rTimes As Range, rCourse As Range, c As Range
Dim colTimes As Long, colCourse As Long
Dim cCourses As Collection
Dim LastRow As Long
Dim i As Long
With WorksheetFunction
colTimes = .Match("Time", Range("$1:$1"), 0)
colCourse = .Match("Course", Range("$1:$1"), 0)
End With
LastRow = Cells(Rows.Count, 1).End(xlUp).row
Set rTimes = Range(Cells(2, colTimes), Cells(LastRow, colTimes))
Set rCourse = Range(Cells(2, colCourse), Cells(LastRow, colCourse))

'get list of courses
Set cCourses = New Collection
On Error Resume Next
For Each c In rCourse
cCourses.Add Item:=c.text, Key:=c.text
Next c
On Error GoTo 0

If cCourses.Count > 999 Then
MsgBox ("Too many courses" & vbLf & "Cannot have more than 999")
Exit Sub
End If
For i = 1 To cCourses.Count
For Each c In rTimes
If c.Offset(columnoffset:=rCourse.Column - rTimes.Column) = cCourses(i) Then
c.Value = c.Value + i / 86400000
End If
Next c
Next i
End Sub
Sub ResetTimes()
Dim rTimes As Range, c As Range
Set rTimes = Cells(2, WorksheetFunction.Match("Time", Range("$1:$1"), 0))
Set rTimes = Range(rTimes, Cells(Rows.Count, rTimes.Column).End(xlUp))

For Each c In rTimes
If IsNumeric(c.Value) Then
c.Value = CDate(c.Value)
End If
Next c

End Sub
'-----------------------------------------------------------------

Sub Color4New2011()
Dim rTimes As Range, rValues As Range, c As Range
Dim rCourse As Range
Const NumToColor As Long = 4
Dim APOffset() As Long
Dim tTimes() As Variant, dPVals() As Double
Dim collTime As Collection, collColQ As Collection
Dim bLowest As Boolean
Dim i As Long, j As Long, k As Long

On Error Resume Next

Set rTimes = Application.InputBox(Prompt:="Select the Times", _
Default:=Selection.Address, Type:=8)
If rTimes Is Nothing Then Exit Sub

Set rValues = Application.InputBox("Select One (1) cell in each column of Values ", Type:=8)
If rValues Is Nothing Then Exit Sub

On Error GoTo 0

'Make times unique to each track
ResetTimes 'just to be safe
IncreaseTimes

bLowest = IIf(MsgBox("Lowest " & NumToColor & "?", vbYesNo) = vbYes, True, False)

ReDim APOffset(0 To rValues.Count - 1)
i = 0
For Each c In rValues
APOffset(i) = c.Column - rTimes.Column
i = i + 1
Next c

'Unique list of times
Set collTime = New Collection
On Error Resume Next
For Each c In rTimes
collTime.Add Item:=c.Value, Key:=CStr(c.Value)
Next c
On Error GoTo 0

ReDim tTimes(0 To collTime.Count - 1, 0 To 2)
For i = 0 To collTime.Count - 1
tTimes(i, 0) = collTime(i + 1)
Next i

Application.ScreenUpdating = False
For k = 0 To UBound(APOffset)
'unique list of rValues values for each time
For i = 0 To UBound(tTimes, 1)
Set collColQ = New Collection
On Error Resume Next
For Each c In rTimes
If c.Value = tTimes(i, 0) Then
With c.Offset(columnoffset:=APOffset(k))
If bLowest = True Then collColQ.Add Item:=.Value, Key:=CStr(.text)
If bLowest = False And .Value <> 0 Then collColQ.Add Item:=.Value, Key:=CStr(.text)
End With
End If
Next c
On Error GoTo 0
If collColQ.Count > 0 Then
ReDim dPVals(0 To collColQ.Count - 1)
For j = 0 To UBound(dPVals)
dPVals(j) = collColQ(j + 1)
Next j
End If
With WorksheetFunction
If bLowest Then
tTimes(i, 1) = .Small(dPVals, .Min(UBound(dPVals) + 1, NumToColor))
tTimes(i, 2) = .Min(dPVals)
Else
tTimes(i, 1) = .Large(dPVals, .Min(UBound(dPVals) + 1, NumToColor))
tTimes(i, 2) = .Max(dPVals)
End If

End With
Next i

'color the cells
For i = 0 To UBound(tTimes, 1)
For Each c In rTimes
If c.Value = tTimes(i, 0) Then
With c.Offset(columnoffset:=APOffset(k))
If bLowest = False Then
'Select Case CDbl(.Text)
Select Case .Value
Case Is = tTimes(i, 2)
.Interior.Color = vbYellow
.Font.Color = vbRed
Case Is >= tTimes(i, 1)
.Interior.Color = vbGreen
.Font.Color = vbRed
Case Else
.Interior.Color = xlNone
.Font.Color = vbBlack
End Select
ElseIf bLowest = True Then
'Select Case CDbl(.Text)
Select Case .Value
Case Is = ""
.Interior.Color = xlNone
.Font.Color = vbBlack
Case Is = tTimes(i, 2)
.Interior.Color = vbYellow
.Font.Color = vbRed
Case Is <= tTimes(i, 1)
.Interior.Color = vbGreen
.Font.Color = vbRed
Case Else
.Interior.Color = xlNone
.Font.Color = vbBlack
End Select
End If
End With
End If
Next c
Next i
Next k

'reset times to baseline
ResetTimes
Application.ScreenUpdating = True
End Sub
===================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top