Conditional Formatting Problem/Bug?

This is my first time in a forum. I have mostly found all the answers I could but I can't seem to find the answer for this:

I don't know if this is a bug in excel or if I am doing something wrong.

I start a new Excel application from another workbook and apply a conditional format that uses an array formula for its condition (I have read that conditional formatting always formulates in array). The conditional format doesn't apply. I need to select the cells with the conditional format, goto to the Conditional Formatting toolbar and reapply it manually. That is, I actually have to select the cells then goto Format->Conditional Format-> I see the formula applied and click OK and the conditional format works.

Why doesn't the format apply from code? Am I doing something wrong? Here is my code. Thanks for your help.

Code Snippet

Dim objExcel As Application
Dim objxlWkBk As Workbook
Dim objxlSheet As Worksheet

Set objExcel = CreateObject("Excel.Application")

Set objxlWkBk = objExcel.Workbooks.Add

Set objxlSheet = objxlWkBk.Sheets(1)

objxlSheet.Range("A1").Value = 1
objxlSheet.Range("A2").Value = 2
objxlSheet.Range("A3").Value = 2
objxlSheet.Range("A4").Value = ""
objxlSheet.Range("A5").Value = 5

With objxlSheet.Range("A1:A5")
objxlSheet.Range("A1").Activate
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(A1<>"""",SUM((A1=$A$1:$A$5)*1)>1)"
.FormatConditions(1).Interior.ColorIndex = 40
End With

objExcel.Visible = True
Set objxlSheet = Nothing
Set objxlWkBk = Nothing
Set objExcel = Nothing

[1909 byte] By [FaV1105] at [2008-1-10]
# 1

I don't think you're doing anything wrong. I was easily able to replicate the same (on Excel 2000, so assuming you're using something more recent this is not a new phenomenon). The VBA code assigns the conditional format formula and formats, but it doesn't seem to actually apply them until you interactively apply the conditional format.

It appears to be related to the array formula in the conditional format. If you replace it with a non-array formula then it works fine. For example, the following non-array equivalent to your formula works OK:

Formula1:="=AND(A1<>"""",COUNTIF($A$1:$A$5,A1)>1)"

That doesn't solve the underlying problem of how to apply array formulae in conditional formatting via VBA. I tried a few things to try to make it refresh or redraw, without success. Even applying a second conditional format applies the second format but doesn't "activate" the first. I think it's likely that this is a bug.

CringingDragon at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 2

Cringing Dragon, Thank you for your prompt reply.

Yes, what you suggested does not solve the underlying problem but you have given me an alternative solution to my problem. I couldn't think of others ways to give me the same result and the COUNTIF function did that for me so thank you so very much. It is really appreciated.

I am still curious why the SUM function didn't work the way from VBA but does when you do it manually.

Thanks again.

FaV1105 at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 3

It should work fine.

It seems like you're not applying the .Select property to let the code know how or where to apply the code.

Try

objxlSheet.Range("A1:A5").Select

Then all your with block

Adam

AdamD.Turner at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 4

Adam, I tried adding the .select code you suggested before the with block and it still doesn't work. Am I doing something wrong?

FaV1105 at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 5

Ok I'm confused. I ran the code you provided and it worked fine.

Which version of Office are you using? I tested your code on 2003.

Adam

AdamD.Turner at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 6

Adam, I am using 2003 as well. Cringing Dragon had the same problem as me. Not sure what the problem is.

FaV1105 at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...