Formula to count a column based on color, but ignore blank with same color

RyOlson

New Member
Joined
Mar 23, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Goal:
I found this in searching for a formula that could count shaded cells based on their color and it worked great, but if I merge cells it will apply the cell color to them and thus throw off my count. How can I count the cell if its yellow, but ignore if its yellow and blank? I am using VBA and Excel 2016. Thank you for any help provided.

Appearance:
1648061211322.png


Module:
Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try modifying the line If rCell.Interior.ColorIndex = lCol Then to
VBA Code:
If rCell.Interior.ColorIndex = lCol and rCell.value <> "" Then

Bye
 
Upvote 0
Try modifying the line If rCell.Interior.ColorIndex = lCol Then to
VBA Code:
If rCell.Interior.ColorIndex = lCol and rCell.value <> "" Then

Bye
I corrected the line within VBA and no change was produced. Does the formula need to be modified?
=ColorFunction(B152,$C$7:$C$150,FALSE)
 
Upvote 0
This is a good example of why merged cells should never be used in Excel - they cause all sorts of issues with things like VBA and sorting.
Most experienced programmers will never use them. They are the probably one of the (if not the) worst feature of Excel and should be avoided whenever possible!

That's not saying that these type of things are impossible in all cases, but they are a whole heck of a lot harder than they should be.
 
Upvote 0
I corrected the line within VBA and no change was produced. Does the formula need to be modified?
I modified the code and now get, for example, the result you see in the picture
A12:B12 are merged cells.

The code now is (the modifiled portion):
VBA Code:
Else
    For Each rCell In rRange
        If rCell.Interior.ColorIndex = lCol And rCell.Value <> "" Then   '<<<
            vResult = 1 + vResult
        End If
    Next rCell
End If

Bye
 

Attachments

  • Merge_Count.jpg
    Merge_Count.jpg
    30.9 KB · Views: 10
Upvote 0
I modified the code and now get, for example, the result you see in the picture
A12:B12 are merged cells.

The code now is (the modifiled portion):
VBA Code:
Else
    For Each rCell In rRange
        If rCell.Interior.ColorIndex = lCol And rCell.Value <> "" Then   '<<<
            vResult = 1 + vResult
        End If
    Next rCell
End If

Bye
I am not sure if it makes a difference, but judging from the original image posted by the OP, it looks to me like the merge cells are happening down rows (i.e. A2:A4) and probably not across columns (A12:B12).

I haven't taken the time to set up an example like they have (they didn't use XL2BB so I cannot simply copy and paste their example) and try your code, so I don't know if that code will have "vertical" merges instead of "horizontal" merges.
 
Upvote 0
Hi Joe,
I agree that missing a common testbed make the discussion timewasting

The OP code includes twice the portion If rCell.Interior.ColorIndex = lCol Then; the one to be modified is the second occourrence (the first one maybe modified, but don't impact the calculation); so my guess is that he modified only the first occourrence.
Let's wait for Ryolson comment...

Bye
 
Upvote 0
That modified portion solved my issue. Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top