You are here:

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

Question
QUESTION: Dear Jan,

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:
A
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.

Thanks
Shoaib

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:
=SUM(\$A\$1:A1)<=100
- set formatting.

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

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

Regards,
Shoaib

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
Next
End Sub
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Appreciated...Thank you so much Jan... This is what I need..

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

#### Jan Karel Pieterse

##### Expertise

Excel and Excel/VBA questions

##### Experience

Excel MVP

Organizations
Self employed Excel developer

Education/Credentials
Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

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