Is it possible to use the CountIf function with advanced options: count cells containing a specific string only if the cell background is of a specific color.
I'm using the Excel formula: `=COUNTIF(page001!B:B;"id-p01"), but blocks of data on each sheet have unique strings, each block could have two different background colors: GREEN or BLUE. So what i'm asking is if i can get a function which would e.g. COUNT cells containing "id-p01" on a selected sheet, but ONLY those with a GREEN background color.
Here is an example of how the sheet looks like:
With this formula: =COUNTIF(page001!B:B;"*id-p01*")
It counts id-p01 on the selected sheet in the B:B column.
Is it possible to make it count only GREEN background colored cells?
This quick solution will print out on the screen the number of cells within the Range B1 to B1000 (you can modify the Range if you've more/less rows to test) that have exactly your green color.
Note that you have to use a macro to do this, it can't be achieved with a simple formula.
To create a macro, press ALT + F11, then right-click on your Workbook's name and "Insert Module". Copy paste the code below and press F5 while you're still in the VBA window or use any other method to run the macro.
Sub CountWithColor()
For Each c In Range("B1:B1000")
If c.Value Like "*id-p01*" And c.Interior.Color = RGB(226, 239, 218) Then
compteur = compteur + 1
End If
Next c
MsgBox (compteur)
End Sub
Let me know if this helped.
Eleove
Count If Value And Color
Function CIVAC(Range As Range, Value As Variant, _
Optional ColorIndex As Long = -4142, _
Optional Compare As Integer = 1) As Long
'Title
'Count If Value And Color
'Description
'In a specified contiguous range, counts the number of cells both,
'containing a specified value and having a specified Interior ColorIndex.
Dim arrVal As Variant 'Range Array
Dim arrClr() As Long 'ColorIndex Array
Dim lngVal As Long 'Row Counter
Dim iVal As Integer 'Column Counter
Dim lngResult As Long 'Result Accumulator
'Values
arrVal = Range.Areas(1) 'Prevent Multiple Areas Error
'ColorIndexes
ReDim arrClr(LBound(arrVal) To UBound(arrVal), _
LBound(arrVal, 2) To UBound(arrVal, 2))
For lngVal = LBound(arrClr) To UBound(arrClr)
For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
arrClr(lngVal, iVal) = Range.Cells(lngVal, iVal).Interior.ColorIndex
Next
Next
'Count
For lngVal = LBound(arrClr) To UBound(arrClr)
For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
If Not IsError(arrVal(lngVal, iVal)) Then 'Prevent VBA Errors
If InStr(1, arrVal(lngVal, iVal), Value, Compare) <> 0 And _
arrClr(lngVal, iVal) = ColorIndex Then lngResult = lngResult + 1
End If
Next
Next
CIVAC = lngResult
End Function
That's nice, but what's the 'Interior ColorIndex' of the color in this cell?
Cell Interior Color Index
Function CICI(CellRange As Range) As Long
'Title
'Cell Interior Color Index
'Description
'Returns the Interior ColorIndex of a specified cell ('CellRange').
'If 'CellRange' contains more than one cell, it uses the first cell.
CICI = CellRange(1, 1).Interior.ColorIndex
End Function
Related
Im looking for a function/macro that finds the next red cell in column A on my worksheet. Basically what I wanna do is Loop through Column A and everytime I find an empty cell, jump down to the next red cell and continue the loop there. All I need is the row# of the next red cell in order to do thuis. What I have working so far is this:
'Loop through column A from top down starting in row 6 and create Chart for each row
For rownumber = 6 To LastRow Step 1
'If Cell is filled
If TPsheet.Cells(rownumber, 1) <> "" Then
'Create Chart
Call CreateChart()
Else
rownumber = rownumber + 2 (This is the problem area, just going down 2 rows sadly doesnt work as sometimes there are 3/4/5 consecutive empty rows, need to set this to rownumber of next red cell)
End If
Next rownumber
The whole table looks like this:
And the amount of empty rows between the different red tables sections can vary, which is why just going down a certain amount of rows when it finds an empty one (my current approach) doesnt work.
For findind a red cell I successfully use this in antoher macro:
Do Until i = 1000
If TPsheet.Range("A" & i).Interior.Color = RGB(255, 0, 0) Then
'...
End If
i = i + 1
Loop
TLDR: Need the rownumber of the next red cell in column a while looping through the column when a cell is empty in order to jump to the next red table header
You can use the build-in Excel Find-function to search for formatting. The following function will return the next cell having a specific back color or Nothing if no cell can be found below a certain point.
Function FindColoredCell(ws As Worksheet, searchColor As Long, Optional afterCell As Range = Nothing) As Range
If afterCell Is Nothing Then Set afterCell = ws.Cells(1, 1)
' Set the search parameter for the specific color.
With Application.FindFormat.Interior
.Pattern = xlSolid
.color = searchColor
End With
' Do the search
Dim cell As Range
Set cell = ws.Cells.Find(What:="", after:=afterCell, SearchDirection:=xlNext, SearchFormat:=True)
If cell Is Nothing Then Exit Function ' now cell found with color
If cell.row < afterCell.row Then Exit Function ' now cell below "afterCell", Search started at top
Set FindColoredCell = cell
End Function
(if it fits your need better, change return value to Long and return cell.Row)
Note: In your code, you are using a For-Loop. Don't do that if you will modify the counting variable (in your case rowNumber) within the loop or you will get unpredictable results. Use do Do While-Loop instead.
This will take a lot of time if you are already over the last red line.
Function GetNextRedInteriorRowNumber() As Long
Dim i As Long
Dim c As Long
Dim retVal As Long
retVal = -1
c = ActiveCell.Column
For i = ActiveCell.Row + 1 To ActiveSheet.Rows.Count
If ActiveSheet.Cells(i, c).Interior.Color = RGB(255, 0, 0) Then
retVal = i
Exit For
End If
Next
GetNextRedInteriorRowNumber = retVal
End Function
I'm looking for an Excel formula that checks if there is any colored cell within a row.
If there is, in the same row in column 2 (Which is actually column B) Print "Yes", otherwise print "No".
I tried this formula to check in the second row:
=IF(2:2.Interior.Color=4,"Yes","No")
Maybe the solution should be written in VBA code rather than Excel formula.
Here is how I want it to look:
In vba you can try with this code: In this exemple i used The range C2 TO F10, but you can change the values that are into startRow, endRow, startCol end in endCol to change the range.
Sub MyInteriorColor()
Const COLOR As Integer = 4 ' here you can change the color
Dim startRow, endRow, startCol, endCol, i, c As Integer
startRow = 2 ' Read the interior color from second row
endRow = 10 ' Last row where i read the interior color
startCol = 3 ' Column where i start to read the interior color
endCol = 6 ' Column where i finish to read interior color
For i = startRow To endRow
For c = startCol To endCol
If Cells(i, c).Interior.ColorIndex = COLOR Then
Cells(i, 2) = "YES"
Exit For
Else
Cells(i, 2) = "NO"
End If
Next c
Next i
End Sub
I think a UDF I probably a better way to do this.
That way each cell can point to whatever range it needs.
Place this code in a module:
Function IsGreen(rng as range)
IsGreen = "No"
for each cell in rng
If cell.Interior.ColorIndex = 4 then
IsGreen = "Yes"
Exit for
end if
next cell
End function
And to use this you enter the formula in column 2:
=IsGreen(C2:G2)
And it should return yes/no.
I typed this answer on my phone so I have not been able to test it but I believe it will work.
I have some code by a former coworker that counts merged cells with a particular string. For example, if there was a merged cell with size 3 with the name "Youtube" on it, it would return 3.
This is the function in question:
Function MergedCellsCount(rRange As Range, crit As Variant) As Double
Application.Volatile
MergedCellsCount = 0 'in case there are no matches
For Each c In rRange
If LCase(c.Value) = LCase(crit) Then
MergedCellsCount = MergedCellsCount + c.MergeArea.Cells.Count
prev_rng = c.MergeArea.Address
End If
Next
' MergedCellsCount = MergedCellsCount / 5
End Function
But now, i want to count the cells that have a substring within that string.
If for example there was a merged cell with size 3 with "Youtube | Spotify", I want to know how to change that function to search for the substring "Youtube".
Any suggestions on how to achieve this?
Thanks in advance!
Count Cells Containing a String (Merged Cells) (UDF)
Note that if you merge or unmerge cells within the Criteria Range, the cells count will not be updated until the next recalculation of the worksheet.
To trigger recalculation, a nice 'trick' is to autofit a column by double clicking the right border of its column header (e.g. for column A, the short line between A and B).
The Code
Option Explicit
Function MergedCellsCount(CriteriaRange As Range, _
Criteria As String) _
As Long
Application.Volatile
Dim c As Range
For Each c In CriteriaRange.Cells
If Not IsError(c) Then
If InStr(1, c.Value, Criteria, vbTextCompare) > 0 Then
MergedCellsCount = MergedCellsCount + c.MergeArea.Cells.Count
End If
End If
Next
End Function
I am trying to count the number of colored cells (which also satisfy another condition).
My cells are as follows:
My intention is to count the cells where there is a 'B' and where the adjacent cells are green in color.
I also write a function as follows:
Function CheckColor(rng As Range) As Boolean
If rng.Interior.ColorIndex = 43 Then
CheckColor = True
Else
CheckColor = False
End If
End Function
I then use SUMPRODUCT function as follows:
=SUMPRODUCT(--(V40:V50="B");--CheckColor(W40:W50))
However, I get an error #VALUE!
UPDATE
I have modified my formula as follows:
Function CheckColor(rng As Range) As Variant
Dim arr As Variant
Dim n As Integer
ReDim arr(0 To rng.Count - 1) As Variant
n = 0
For Each cell In rng
If cell.Interior.ColorIndex <> 43 Then
bl = False
Else
bl = True
End If
arr(n) = bl
n = n + 1
Next cell
CheckColor = arr
End Function
And I use the formula as follows:
=SUMPRODUCT((V40:V50="B")*CheckColor(W40:W50))
The answer I get is 6, which is wrong.
The arrays for column ranges are a bit different Variant(1 To 11, 1 To 1)
Function CheckColor(rng As Range)
Dim arr()
ReDim arr(1 To rng.Count, 1 To 1)
' arr = rng.Value2 ' arr Type in the Locals window shows as Variant(1 To 11, 1 To 1)
For i = 1 To rng.Cells.Count
arr(i, 1) = rng.Cells(i, 1).Interior.ColorIndex = 43
Next i
CheckColor = arr
End Function
You can do this without VBA, but you'll need a 'helper' column.
Create a named range with the name CellColour and the formula =GET.CELL(63,Sheet1!$B1)
Using your example (assuming it starts in cell A1), enter this formula in cells C1:C11: =CellColour. By the looks of your screen shot it should return 43 for green.
You can then use this formula to count column A with green in column B:
=COUNTIFS($A$1:$A$11,"B",$C$1:$C$11,43)
Background colour: How to count cells in a range with a value less than another cell in excel?
Font colour: Excel formula to get cell color
Edit, correction
In the function, you give the funtion a range and compare it to a ColorIndex. That means you ask if the whole range have the colorindex not the cells between.
What you do would work if Excel automaticly reference the right cells and compare them to the value, but i wouldn't do that for you.
So there are different ways you actually can manage this. First you do it without vba like Darren Bartrup-Cook meantion, you can do it with a helper column and write it like
=If(V40="B";CheckColor(W40)
And count the with Countif the true values or you write it in VBA but then you need to loop trough the cells one by one like this:
For x = 1 to 50
If Cells(x,10).Value = "b" AND Cells(x,11).ColorIndex = 43 Then
counter = counter + 1
Next x
Endif
I want to match the employee name on sheet one against employee names in sheet two, then run a count of all yellow-colored (filled) cells in a particular column.
I have a VBA module that will run the count of highlighted cells without doing a name match and it works perfectly. Now I need to add in an additional metric of running a count of all highlighted cells for each employee.
Data Info:
Sheet One B2:B50 - list of employee last names.
Sheet Two D2:D1845 - column with employee last names. Note: This is a worksheet with 1845 line items of client data records and therefore the employee name could be listed numerous times in said column.
Sheet Two E2:E1845 - column with yellow-colored cells. Not all cells in the column are colored yellow. Which is why I need a count of how many are colored for each employee.
Count by color VBA that works:
Function CountByColor(InputRange As Range, ColorRange As Range) As Long
Dim cl As Range, TmpCount As Long, ColorIndex As Integer
Application.Volatile
ColorIndex = ColorRange.Interior.ColorIndex
TmpCount = 0
On Error Resume Next
For Each cl In InputRange.Cells
If cl.Interior.ColorIndex = ColorIndex _
Then TmpCount = TmpCount + 1
Next cl
CountByColor = TmpCount
End Function
Based on what you explained to me you want in the comments here's a one-liner to do what you want:
Public Sub NameColorCount(NameToSearch As String, TargetCell As Range, _
SearchRange As Range, RangeToCountColor As Range, ColorRange As Range)
If Not SearchRange.Find(NameToSearch) Is Nothing Then
TargetCell.Value = CountByColor(RangeToCountColor, ColorRange)
End If
End Sub
If you want to do this in a cell you can use CountByColor as a UDF and use the following formula:
=IF(COUNTIF(D:D, B1)>0,CountByColor(E:E, B1),"")
Assuming your ColorRange is the 'B' cell, modify otherwise
I'm not really sure if this is what you want to achieve I can't post images yet.
This Sub insert a counter in range Sheet1 C2:C50 for every employee who is in Sheet2 D2:D1845 and the cell next to is yellow colored.
Sub Find_Matches()
Dim CompareRange As Variant, x As Variant, y As Variant, CountA As Integer
Set EmployeeRange = Worksheets("Sheet1").Range("B2:B50")
Set CompareRange = Worksheets("Sheet2").Range("D2:D1845")
For Each x In EmployeeRange
For Each y In CompareRange
If x = y And y.Offset(0, 1).Interior.ColorIndex = 6 Then CountA = CountA + 1
Next y
x.Offset(0, 1).Value = CountA
CountA = 0
Next x
End Sub