1

I need a macro to be used in conditional formatting. I need that if a row contains a cell that is 1, then i need that the whole row is highlighted (e.g. A1 - E1).

at the moment i only managed to highlight the cells containing number 1, but not the whole row. any ideas ?

thanks in advance

1 Answer 1

3

To do it in Excel, select A1:E1, then Format > Conditional Formatting... > Formula Is > =SUMIF($A1:$E1,"=1")>0. Don't forget the $ dollar signs, to specify an absolute (and not a relative) reference to columns A to E!

This is the VBA code to do the same thing in a macro:

With Range("A1:E1")
    ' If you need to delete any "pre-existing conditions" 
    ' (no US healthcare reform pun intended) then uncomment the following line: 
    '.FormatConditions.Delete 

    .FormatConditions.Add Type:=xlExpression, _
         Formula1:="=SUMIF($A1:$E1,""=1"")>0"
    .FormatConditions(1).Interior.ColorIndex = 6 ' yellow background
End With
3
  • from the comments and the question itself I understand that the criteria was the following "if is there any '1' in the row colour the entire row/range" not "if all the values sums 1 then color" so the formulae has to be changed to =IF(COUNTIF($A$1:$E$1;"=1")>0;TRUE;FALSE) in FormatConditions.
    – Alen
    Aug 2, 2011 at 19:07
  • My formula does what the OP asks -- it does not do "if all the values sums 1 then color" as you seem to suggest. To convince yourself, try it out! True, your formula works just as well. Aug 3, 2011 at 5:38
  • True, looks like a miss a IF in my earlier inspection, my apologies
    – Alen
    Aug 3, 2011 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.