Forum Discussion
CarolSweet
Jan 09, 2024Copper Contributor
Formula or Macro help - using colours to get a total sum??
Hi I don't know what the correct terminology is to search for help on which formula/function/macro I need to use so that I can allocate individual colours a value (cost) and then get those colours ...
ItsBhatti
Jan 09, 2024Iron Contributor
Certainly! If you want to use colors to calculate a total sum in Excel, you can achieve this with the help of a formula or a macro. Here's a simple way to do it using a formula:
Using Formulas:
Assuming you have a column of numbers with corresponding colors in another column:
Assign Numbers and Colors:
- Assign numbers to cells in one column and colors (e.g., using conditional formatting) to cells in another column.
Use the SUM Function with CELL and GET.CELL:
- Suppose your numbers are in column A and colors are in column B.
- In an empty cell, use the following formula:This formula sums the numbers in column A where the corresponding cell in column B has a specific color. In this example, color code 3 represents a specific color.excelCopy code=SUM(IF(GET.CELL(63,INDIRECT("RC[-1]",0))=3,INDIRECT("RC[-2]",0),0))
Adjust for Your Color:
- Replace the 3 in the formula with the color code corresponding to your desired color.
Using VBA Macro:
If you prefer a macro solution, you can use the following steps:
Open the Visual Basic for Applications (VBA) Editor:
- Press Alt + F11 to open the VBA Editor in Excel.
Insert a New Module:
- In the VBA Editor, right-click on any item in the Project Explorer, choose "Insert," and then select "Module."
Enter the Macro Code:
- Copy and paste the following VBA code into the module:vbaCopy codeFunction SumByColor(rngSumRange As Range, cellColor As Range) As Double Dim sumValue As Double Dim cell As Range Application.Volatile sumValue = 0 For Each cell In rngSumRange If cell.Interior.Color = cellColor.Interior.Color Then sumValue = sumValue + cell.Value End If Next cell SumByColor = sumValue End Function
- Copy and paste the following VBA code into the module:
Use the Macro Function:
- Back in your Excel workbook, you can use the new function like a regular Excel function. For example, if you want to sum numbers in column A with a specific color in column B:Replace B1 with a cell that has the color you want to use for summationhttps://baitmeetz.co.il/excelCopy code=SumByColor(A:A, B1)
- Back in your Excel workbook, you can use the new function like a regular Excel function. For example, if you want to sum numbers in column A with a specific color in column B:
Remember to save your workbook as a macro-enabled workbook if you're using VBA.
Choose the method that suits your preference – whether it's using a formula or a VBA macro – and adapt it to your specific Excel setup.