You are here:

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

Advertisement


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 ----------

QUESTION: Thanks for your reply...

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

Regards,
Shoaib

Answer
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
   Next
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

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


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, ...

©2016 About.com. All rights reserved.