Countifs based on text and Color

andrewc85

New Member
Joined
Nov 28, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am needing a formula to perform a countifs if Column A contains "Andrew" and Column B has the interior.colorindex =35

I have been using the below function which I think is quite commonly found on the web, and works fine but only counts column B and has no reference to column A or any other criteria.

VBA Code:
Public Function GetColorIndex(Cell As Range)
  GetColorIndex = Cell.Interior.ColorIndex
End Function

Public Function CountColorIndexInRange(Rng As Range, TestColor As Long)
  Dim cnt
  Dim cl As Range
  cnt = 0

  For Each cl In Rng
    If GetColorIndex(cl) = TestColor Then
      Rem Debug.Print ">" & TestColor & "<"
      cnt = cnt + 1
    End If
  Next

  CountColorIndexInRange = cnt

End Function


Sheet11.Range("D4").Value = CountColorIndexInRange(Sheet11.Range("B7:B750"), 35)

I just need to help modifying the above formula to also incrportate a second condition for column A. There is also a sample screen shot below.

Many help would be greatly appreciated.

Cheers, Andrew


Capture.PNG
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try replacing the only IF statement with the following:

VBA Code:
If GetColorIndex(cl) = TestColor  And InStr(cl.Offset(0,-1),"Andrew",1) > 0 Then
 
Upvote 0
Many thanks for the suggestion, but am now receiving Run-Time error 5. Invalid procedure call or argument.

I have the below sub to run the function which is when the error occurs. 35 references the colorindex.

Excel Formula:
Sub Test()
Sheet11.Range("D4").Value = CountColorIndexInRange(Sheet11.Range("N7:N750"), 35)

End Sub
 
Upvote 0
In the original post, you passed column B and wanted to check whether column A contained the word "Andrew". Now, you are passing column N. So, the offset is not correct. The offset is now referring to column M, not A. Even so, the error message, "invalid procedure call or argument", doesn't make sense. It seems to be saying the syntax of InStr call is incorrect. What'd you get if you do a debug print on InStr(cl.Offset(0,-1),"Andrew") or better yet, InStr(cl.Offset(0,-13),"Andrew")?
 
Upvote 0
Sorry for the confusion, lets talk columns A & B as per previous message.

Unfortunately, still getting the same "invalid procedure call or argument", when running the below code.

VBA Code:
Sub Test()
Sheet11.Range("D4").Value = CountColorIndexInRange(Sheet11.Range("A7:A750"), 35)
End Sub

Public Function GetColorIndex(Cell As Range)
  GetColorIndex = Cell.Interior.ColorIndex
End Function

Public Function CountColorIndexInRange(Rng As Range, TestColor As Long)
  Dim cnt
  Dim cl As Range
  cnt = 0

  For Each cl In Rng
    If GetColorIndex(cl) = TestColor And InStr(cl.Offset(0, -1), "Andrew", 1) Then
      Rem Debug.Print ">" & TestColor & "<"
      cnt = cnt + 1
    End If
  Next

  CountColorIndexInRange = cnt


End Function
 
Upvote 0
Even changing the referenced cells to the below, I still get the error.

VBA Code:
Sub Test()
Sheet11.Range("D4").Value = CountColorIndexInRange(Sheet11.Range("B7:B750"), 35)
End Sub
 
Upvote 0
I solve the issue, the code should have been

VBA Code:
If GetColorIndex(cl) = 35 And cl.Offset(0, -1).Value = "Andrew" Then

Many thanks for all your help getting me on the right track, its greatly appreciated!
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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