Forum Discussion
BrianWW
Jun 01, 2022Copper Contributor
Sum a currency column, stop at a certain value, change color
Hi All,
I have a living worksheet that is updated anywhere from weekly to daily. I have a column that contains only currency values, and I need to sum the column from the top, and stop once I hit a certain number, and color those cells a specific color.
For example, there are 100+ rows of data, 26+ columns, one column is all currency values, I need to sum it, from the top, until I get to $80k and create a conditional format that will highlight only those cells a certain color, and will update itself as rows are added and deleted manually.
Please note that I am fairly an Excel noob/self taught off Google and YouTube, so you might have to walk me through this, if its possible.
Let's say the currency amounts are in D2:D150.
Select this range. D2 should be the active cell in the selection,
On the home tab of the ribbon, select Conditional Formatting > New Rule...
Select 'Use a formula to determine which cells to format'.
Enter the formula =SUM(D$1:D1)<80000
Click Format...
Activate the Fill tab.
Select a color.
Click OK, then click OK again.
- BrianWWCopper Contributor
HansVogelaar Awesome, thank you for your reply, it worked! Next question, is there away for me to start summing the remaining cells, stopping again at $80000, setting the color, and essentially repeating this through the entire column, so that every $80000 worth of cells is it's own color? Thank you!!!
That'll take a lot of work, since you have to create a separate rule for each tier.
You'd have to create a rule for =SUM(D$1:D1)<160000, a rule for =SUM(D$1:D1)<240000 etc.
Finally, reverse the order of the rules in the Manage Rules dialog, so that the one for 80000 is at the top, then the one for 160000 etc.
If you wish, you and run the following macro to create the rules automatically. Change the constants at the beginning to suit your situation.
Sub CreateRules() Const col = "D" ' column to color Const fr = 2 ' first row Const amount = 80000 ' threshold Dim rng As Range Dim s As Double Dim n As Long Dim i As Long Set rng = Range(Cells(fr, col), Cells(Rows.Count, col).End(xlUp)) s = Application.Sum(rng) n = Application.RoundUp(s / amount, 0) With rng.FormatConditions .Delete For i = 1 To n .Add(Type:=xlExpression, Formula1:="=SUM(" & col & "$" & fr - 1 & _ ":" & col & fr - 1 & ")<" & i * amount).Interior.ColorIndex = i + 2 Next i End With End Sub