You are here:

Excel/Color the Range of Cell when SUM reaches upto 100



I need your assistance in a very easy question.

How can we Highlight the range or cell when sum of that range reaches upto total '100' with the help of VBA Code?

You can see in below example the sum of A1 to A6 is 100. Now I color the range from A1 to A6 only.

For Eg:
1 10
2 10
3 10
4 10
5 30
6 30
7 50
8 60
9 20
10 70

Please provide your assistance on given question. I will be very thankful to you.


ANSWER: You could use a conditional format for this:

Select entire range
- Click conditional formatting, new rule
- Select "use a formula to determine which cells to format"
- enter this formula:
- set formatting.

---------- FOLLOW-UP ----------

QUESTION: Thanks for your reply...

But how can I do with VBA Code to accomplish this task.


How about this?

Sub ColorCellsThatTotal100()
   Dim oCell As Range
   Dim dTot As Double
   Range("A1:A100").Interior.Color = xlNone
   For Each oCell In Range("A1:A100")
       dTot = dTot + oCell.Value
       If dTot = 100 Then
         Range("A1", oCell).Interior.Color = vbRed
         Exit For
       End If
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Jan Karel Pieterse


Excel and Excel/VBA questions


Excel MVP

Self employed Excel developer

Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

Past/Present Clients
Shell, Fortis bank, ABN-AMRO bank, Morgan Stanley, ...

©2016 All rights reserved.