Microsoft Excel

How To Become Better With How to use VBA to sum Excel values by fill color

excel work

The article How to effectively whole qualities by a cell’s experience shading in Excel disclosed how to join shading and implicit channels to aggregate qualities by a cell’s fill shading. It’s a simple method to transform hues into important data. A basic separating strategy won’t generally be satisfactory however, particularly in case you’re reusing the whole in different articulations. In the event that you require a remain solitary aggregate, you can utilize one of two VBA client characterized capacities (UDFs). One depends on an assistant section; the other one doesn’t. Neither requires a great deal of code or concentrated information. On the drawback, both have impediments: Neither UDF works with contingent designing or is dynamic. Be that as it may, if this is the thing that you require, you can likely live with those impediments.

I’m utilizing Excel 2016 (work area) on a Windows 10 64-bit framework, yet both UDFs will work in more established renditions. Nonetheless, the program rendition doesn’t bolster VBA. You can utilize your own particular information or download the show .xlsm, .xls, .cls, and .bas records.

Full scale empowered record

Before we start, make certain to spare the exercise manual you’re utilizing as a full scale empowered record as takes after:

  1. Tap the File tab and pick Save As in the left sheet.
  2. Pick Excel Macro-Enabled Workbook (*.xlsm) from the second dropdown.
  3. Snap Save.

The downloadable exhibit document is as of now in this arrangement. In case you’re utilizing a menu variant of Excel, this progression isn’t essential.

A basic UDF and partner section

In case you’re utilizing direct designing rather than a contingent organizing, you can utilize a straightforward (however constrained) VBA UDF to aggregate qualities by the cell’s fill shading. The example sheet in Figure A demonstrates a couple of direct arrangements in section B: green if the esteem is $1,000 or more prominent and red if the esteem is $200 or less. Presently, in all actuality, you’d most likely utilize contingent arranging in this circumstance, so please excuse the devised case.

Figure A

Excel

The immediate designing in segment B distinguishes the most astounding and least qualities.

The initial step is to include the straightforward UDF appeared in Listing A to your exercise manual. To do as such, press Alt+F11 to open the Visual Basic Editor (VBE). From the Insert menu, pick Module. At that point, enter ColorSum() physically or import the downloadable .bas record. Try not to reorder from this site page.

Posting A

Function ColorSum(CellColor As Range)
    'Sum values by cell's background color.
    ColorSum = CellColor.Interior.ColorIndex
End Function

ColorSum() passes the present cell as a range question and returns the ColorIndex property setting. Enter the accompanying capacity into D2 and duplicate it to the rest of the cells, as appeared in Figure B:

=ColorSum(B2)

Figure B

Excel

Call the UDF.

The UDF references the cells in section B and returns the record an incentive for every cell’s fill shading: Green is 48 and red is 3. We couldn’t care less about the cells with no shading doled out (- 4142).

Next, enter the accompanying SUMIF() in E2 and duplicate it to the rest of the cells:

=SUMIF($D$2:$D$18,$D2,$B$2:$B$18)

As should be obvious in Figure C, the SUMIF() work restores the sums for the green and red cells: 3,600 and 602, individually. The primary range alludes to the shading record esteems returned by the UDF in segment D. The reference to $D2 utilizes the present list an incentive as criteria—we’re searching for list esteems in D2:D18 that match the record an incentive in D2. The last range reference distinguishes the qualities the capacity totals when the initial two qualities coordinate.

Figure C

Excel

SUMIF() restores an aggregate for the green and red cells.

To be perfectly honest, it’s somewhat of a wreck. Despite the fact that it works, you most likely won’t be content with this plan. Figure D demonstrates a more sensible setup. Physically add the fill hues to G1 and H1; we’ll utilize them as header cells and criteria. At that point, enter the accompanying capacity in G1 and duplicate it to H1:

=SUMIF($D$2:$D$18,ColorSum(G$1),$B$2:$B$18)

The principal run alludes to the list esteems returned by the UDF. The second contention, ColorSum(), restores the shading file an incentive for the header cell; for this situation the cell is G1 and the record esteem is 48. At the point when the qualities for the initial two contentions coordinate, this capacity aggregates the relating esteems in B2:B18. On the off chance that you would prefer not to see the record esteems in section D, conceal the segment. Thusly, the qualities in segment E aren’t fundamental.

Figure D

Excel

This straightforward grid streamlines the summing.

An enhanced UDF

The primary UDF relies upon an aide section—the shading file esteems in segment D. You can dispose of this segment utilizing the UDF appeared in Listing B. Figure E demonstrates the consequences of this UDF in the straightforward network, however in push 3. Enter the accompanying capacity into G3 and duplicate it to H3:

=ColorSum2(G$1,$B$2:$B$18)

This UDF utilizes the sentence structure

=COLORSUM2(criteria,range)

where criteria references the shading you’re coordinating and run signifies the qualities you’re summing. More or less, this UDF thinks about the fill shade of the present cell (B2:B18) to the fill shading in the criteria cell (G1). At the point when those qualities coordinate, the capacity includes the incentive in the present cell to lSum. In the wake of checking every one of the cells in the passed run (rng), the UDF restores the last estimation of lSum.

Posting B

Function ColorSum2(CellColor As Range, rng As Range)
    'Sum values by cell's background color.
    Dim lSum As Long
    Dim iIndex As Integer
    Dim cclr As Variant
    iIndex = CellColor.Interior.ColorIndex
    Debug.Print iIndex
    For Each cclr In rng
        If cclr.Interior.ColorIndex = iIndex Then
            lSum = WorksheetFunction.Sum(cclr, lSum)
        End If
    Next cclr
    ColorSum2 = lSum
End Function

Figure E

Excel

This enhanced UDF dispenses with the assistant section.

Dynamic, kind of

Both UDFs work, however nor is dynamic. Figure F demonstrates the aftereffect of changing the shade of B3—neither UDF refreshes. In the wake of evolving, including, or erasing a shading, you should compel the record to recalculate by squeezing Ctrl+Alt+F9. You could set the Volatile property to True in the UDFs, yet despite everything you’d need to press F9; that is very little better—affirm, it’s no better by any stretch of the imagination.

Figure F

Excel

The UDF doesn’t refresh; it’s a one-time bargain.

There’s no simple method to make either UDF dynamic. Posting C contains an occasion method that depends on the Worksheet_SelectionChange occasion. When you select another cell, this system powers the document to compute when the fill shading changes. Once more, enter the code physically or import the downloadable .cls record. This code goes in the sheet’s module, not the module you included before. (You can pick the sheet in the VBE’s Project Explorer.)

Posting C

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Compares color after selection change to force calculaton.
    'Forces UDFs, ColorSum() and ColorSum2() to recalculate
    'when fill colors are changed.
    Static LastRange As Range
    Static LastColorIndex As String
    If LastRange.Cells.Interior.ColorIndex <> LastColorIndex Then
        Application.CalculateFull
    End If
    Set LastRange = Target
    LastColorIndex = Target.Cells.Interior.ColorIndex
End Sub

While it causes, it is anything but a rich arrangement either. You should make sure to move the determination to trigger it, so it’s as yet dangerous. I give this last methodology to be extensive; it doesn’t generally enhance the circumstance on the grounds that whichever way you should make sure to compel the recalculation. Likewise, every time you select an alternate cell, you trigger this method; it will back things off in a bustling sheet. I can’t genuinely prescribe its utilization, yet you choose for yourself. Regardless, I lean toward the Ctrl+Alt+F9 keystroke to the worksheet occasion system. I prescribe searching for an approach to consolidate this UDF in a robotized procedure so you can include a recalculation strategy, wiping out the need to make sure to do as such physically.

BLOGS : Office Com Setup

Leave a Reply

Your email address will not be published. Required fields are marked *