***I apologize for writing in English so that the code and the solution will not be screwed up by the translation tool, if there is anything I need to explain, please correct me***
Hello Alberto Tiberi
Welcome to Microsoft Community!
To accomplish this task in Excel, you can use a combination of conditional formatting (for easily identifying and coloring cells) and a special formula that sums only the cells that are not colored. Excel doesn't provide a direct formula to sum based on cell color in its standard functions. However, you can use a VBA (Visual Basic for Applications) script to create a user-defined function (UDF) that sums cells based on their background color.
Step 1: Insert VBA Code for Summing Non-Colored Cells
- Press Alt + F11 to open the VBA Editor.
- Go to Insert > Module to create a new module.
- Copy and paste the following VBA code into the module:
Function SumByColor(SelectedCells As Range, ColorCriteria As Range) As Double
Dim Sum As Double
Dim Cell As Range
Sum = 0
For Each Cell In SelectedCells
If Cell.Interior.Color = ColorCriteria.Interior.Color Then
Sum = Sum + Cell.Value
End If
Next Cell
SumByColor = Sum
End Function
- Press Ctrl + S to save your VBA project and then close the VBA Editor.
This function, SumByColor, sums the cells that match the background color of a specified "criteria" cell. If you want to sum cells that are not colored, you'll need to slightly modify this function or use it in a specific way, detailed in the next steps.
Step2: **** You can now use this function in your Excel sheet like so:
=SumByColor(Range, CriteriaCell)
- Range is the range of cells you want to sum.
- CriteriaCell is a cell with the background color you want to exclude from the sum.
To sum non-colored cells specifically, you might need a reference cell (CriteriaCell) that has a color you will not use in your data range. Then, subtract the result of this function from the total sum of the range to get the sum of non-colored cells.
Summing Non-Colored Cells:
- If your values are in A1:A10, and you want to exclude, say, red-colored cells, first sum all cells: =SUM(A1:A10).
- Choose or fill a cell (e.g., B1) with the color you want to exclude. This cell is your CriteriaCell.
- Use the custom function to subtract the sum of colored cells: =SUM(A1:A10) - SumByColor(A1:A10, B1).
This setup will give you the sum of cells in A1:A10 that are not colored with the color in B1.
Remember, this VBA script will need to be saved within the workbook where you want to use it, and macros must be enabled for it to work. If you share the workbook, the script needs to be present in the recipient's workbook as well.
Please feel free to let me know if anything is unclear.
Regards,
Clint| Microsoft Community Support Specialist
***Italian language version(Using code blocks will lead to code confusion. If anything is unclear, please feel free to point it out.)***
Ciao Alberto Tiberi
Benvenuto nella comunità Microsoft!
Per eseguire questa attività in Excel, puoi utilizzare una combinazione di formattazione condizionale (per identificare e colorare facilmente le celle) e una formula speciale che somma solo le celle che non sono colorate. Excel non fornisce una formula diretta per sommare in base al colore della cella nelle sue funzioni standard. Tuttavia, puoi utilizzare uno script VBA (Visual Basic for Applications) per creare una funzione definita dall'utente (UDF) che somma le celle in base al colore di sfondo. Passaggio 1: inserisci il codice VBA per sommare le celle non colorate
Premi Alt + F11 per aprire l'editor VBA.
Vai su Inserisci > Modulo per creare un nuovo modulo.
Copia e incolla il seguente codice VBA nel modulo:
Function SumByColor(SelectedCells As Range, ColorCriteria As Range) As Double
Dim Sum As Double
Dim Cell As Range
Sum = 0
For Each Cell In SelectedCells
If Cell.Interior.Color = ColorCriteria.Interior.Color Then
Sum = Sum + Cell.Value
End If
Next Cell
SumByColor = Sum
End Function
Premi Ctrl + S per salvare il tuo progetto VBA e quindi chiudere l'editor VBA.
Questa funzione, SumByColor, somma le celle che corrispondono al colore di sfondo di una cella "criteri" specificata. Se vuoi sommare le celle che non sono colorate, dovrai modificare leggermente questa funzione o usarla in un modo specifico, dettagliato nei passaggi successivi.
Passaggio 2: ora puoi utilizzare questa funzione nel tuo foglio Excel in questo modo:
=SumByColor(Range, CriteriaCell)
Intervallo è l'intervallo di celle che desideri sommare.
CriteriaCell è una cella con il colore di sfondo che desideri escludere dalla somma.
Per sommare in modo specifico le celle non colorate, potresti aver bisogno di una cella di riferimento (CriteriaCell) con un colore che non utilizzerai nell'intervallo di dati. Quindi, sottrai il risultato di questa funzione dalla somma totale dell'intervallo per ottenere la somma delle celle non colorate.
Sommando le celle non colorate:
Se i tuoi valori sono in A1:A10 e desideri escludere, ad esempio, le celle di colore rosso, somma prima tutte le celle: =SOMMA(A1:A10).
Scegli o riempi una cella (ad esempio B1) con il colore che desideri escludere. Questa cella è la tua CriteriaCell.
Utilizza la funzione personalizzata per sottrarre la somma delle celle colorate:
=SUM(A1:A10) - SumByColor(A1:A10, B1)
Questa configurazione ti darà la somma delle celle in A1:A10 che non sono colorate con il colore in B1.
Ricorda, questo script VBA dovrà essere salvato nella cartella di lavoro in cui desideri utilizzarlo e affinché funzioni, le macro devono essere abilitate. Se condividi la cartella di lavoro, lo script deve essere presente anche nella cartella di lavoro del destinatario.
Non esitate a farmi sapere se qualcosa non è chiaro.
Saluti,
Clint| Specialista del supporto della community Microsoft