如何从CountIF中获取与活动单元格的值和颜色相匹配的单元格计数,以及与VBA Excel中CountIF中找到的单元格计数相等的循环代码?

如何从CountIF中获取与活动单元格的值和颜色相匹配的单元格计数,以及与VBA Excel中CountIF中找到的单元格计数相等的循环代码?,vba,excel,Vba,Excel,我还有一个和我最近的问题性质相似的问题。我在VBA中有一个函数,它查找与活动单元格的日期和单元格颜色匹配的单元格,在找到下一个匹配的单元格后,它转到H列中的相应单元格,并将其高亮显示为青色。这正是我想要的,但每次我都必须单击run宏。我想让函数在所有匹配的单元格上工作。我正在考虑使用Do-Until循环,found@,但要做到这一点,我需要知道使循环停止的匹配单元格的数量 我的工作代码: Sub Test1() ' ' Test1 Macro ' ' Dim CellColor As Varia

我还有一个和我最近的问题性质相似的问题。我在VBA中有一个函数,它查找与活动单元格的日期和单元格颜色匹配的单元格,在找到下一个匹配的单元格后,它转到H列中的相应单元格,并将其高亮显示为青色。这正是我想要的,但每次我都必须单击run宏。我想让函数在所有匹配的单元格上工作。我正在考虑使用Do-Until循环,found@,但要做到这一点,我需要知道使循环停止的匹配单元格的数量

我的工作代码:

Sub Test1()
'
' Test1 Macro
'
'
Dim CellColor As Variant
Dim SearchDate As String, FoundAt As String


CellColor = Range("B" & ActiveCell.Row).Interior.Color
SearchDate = Range("B" & ActiveCell.Row).NumberFormat

    Range("B" & ActiveCell.Row).Select
    Application.FindFormat.Clear
    Application.FindFormat.NumberFormat = SearchDate
    Application.FindFormat.Interior.Color = CellColor

    Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
        , SearchFormat:=True).Activate

End Sub
我正在考虑使用的循环代码可以在上面的链接中找到:

Dim i As Integer
i = 1

Do Until i > 6
    Cells(i, 1).Value = 20
    i = i + 1
Loop
如果:

Dim cellCount As Integer
cellCount = Application.WorksheetFunction.CountIf(Range("B1:B30"), "7/22/2016") ‘Count matching 

MsgBox cellCount ‘test to see if count works
但它有两个问题。问题一,我必须在代码中手动输入日期“7/22/2016”,而不是使用变量“SearchDate”。问题二是它只搜索日期,而不是过滤电子表格中日期的单元格颜色

所以我的问题就在这里。如何获得满足活动单元格的日期和颜色值的日期数,并将该数传递给要在循环中使用的变量

如果有更有效的方法来做这一切,请告诉我。非常感谢

参考图片:


如果我正确理解了你的问题,那么这就是你要寻找的

Sub Test1()

Dim CellColor As Variant
Dim SearchDate As String

For i = 1 To 19
    CellColor = Cells(i, 2).Interior.Color
    SearchDate = Cells(i, 2).NumberFormat

    If Cells(i, 2).Value <> "" Then
        For j = i To 19
            If i <> j And CellColor = Cells(j, 2).Interior.Color And SearchDate = Cells(j, 2).NumberFormat Then
                Cells(j, 8).Interior.Color = RGB(0, 255, 255)
            End If
        Next j
    End If
Next i

End Sub
子测试1()
作为变体的暗淡细胞颜色
将搜索日期设置为字符串
对于i=1到19
CellColor=单元格(i,2).Interior.Color
SearchDate=单元格(i,2).数字格式
如果单元格(i,2).值为“”,则
对于j=i到19
如果i j和CellColor=Cells(j,2).Interior.Color和SearchDate=Cells(j,2).NumberFormat则
单元格(j,8).Interior.Color=RGB(0,255,255)
如果结束
下一个j
如果结束
接下来我
端接头

如果我正确理解了你的问题,那么这就是你要找的

Sub Test1()

Dim CellColor As Variant
Dim SearchDate As String

For i = 1 To 19
    CellColor = Cells(i, 2).Interior.Color
    SearchDate = Cells(i, 2).NumberFormat

    If Cells(i, 2).Value <> "" Then
        For j = i To 19
            If i <> j And CellColor = Cells(j, 2).Interior.Color And SearchDate = Cells(j, 2).NumberFormat Then
                Cells(j, 8).Interior.Color = RGB(0, 255, 255)
            End If
        Next j
    End If
Next i

End Sub
子测试1()
作为变体的暗淡细胞颜色
将搜索日期设置为字符串
对于i=1到19
CellColor=单元格(i,2).Interior.Color
SearchDate=单元格(i,2).数字格式
如果单元格(i,2).值为“”,则
对于j=i到19
如果i j和CellColor=Cells(j,2).Interior.Color和SearchDate=Cells(j,2).NumberFormat则
单元格(j,8).Interior.Color=RGB(0,255,255)
如果结束
下一个j
如果结束
接下来我
端接头

下一个函数将对B列中的所有匹配单元格进行计数。 它不会突出显示任何单元格。您必须自己添加此部件

为了使用它,只需在代码中添加以下内容:

Dim x As Integer
x = countMatchingCells
功能:

Function countMatchingCells() As Integer

Dim CellColor As Variant
Dim SearchDate As String, FoundAt As String
Dim i As Integer
Dim counter As Integer

CellColor = Range("B" & ActiveCell.Row).Interior.Color
SearchDate = Range("B" & ActiveCell.Row).NumberFormat
counter = 0
'assuming we are working on sheet1:
For i = 1 To Sheets(1).Cells(Sheets(1).Rows.Count, 2).End(xlUp).Row
If Cells(i, 2).Interior.Color = CellColor And _
Cells(i, 2).NumberFormat = SearchDate Then
countMatchingCells = countMatchingCells + 1
'you can write more code here, for example if you want to highlight cells once you found a match
End If
Next i
End Function

下一个函数将统计B列中所有匹配的单元格。 它不会突出显示任何单元格。您必须自己添加此部件

为了使用它,只需在代码中添加以下内容:

Dim x As Integer
x = countMatchingCells
功能:

Function countMatchingCells() As Integer

Dim CellColor As Variant
Dim SearchDate As String, FoundAt As String
Dim i As Integer
Dim counter As Integer

CellColor = Range("B" & ActiveCell.Row).Interior.Color
SearchDate = Range("B" & ActiveCell.Row).NumberFormat
counter = 0
'assuming we are working on sheet1:
For i = 1 To Sheets(1).Cells(Sheets(1).Rows.Count, 2).End(xlUp).Row
If Cells(i, 2).Interior.Color = CellColor And _
Cells(i, 2).NumberFormat = SearchDate Then
countMatchingCells = countMatchingCells + 1
'you can write more code here, for example if you want to highlight cells once you found a match
End If
Next i
End Function

如果我理解了你的问题,那么在屏幕截图中,B4的匹配在B8和B13中找到。因此,H8和H13应以青色突出显示。这就是你想要的吗?Sorta,我想找到活动单元格的单元格数。所以如果B4是激活的细胞,我想找到等于激活细胞的细胞数。在这种情况下,计数是3。如果我理解了你的问题,那么在屏幕截图中,只要B4与B8和B13匹配。因此,H8和H13应以青色突出显示。这就是你想要的吗?Sorta,我想找到活动单元格的单元格数。所以如果B4是激活的细胞,我想找到等于激活细胞的细胞数。在这种情况下,计数是3。如果可以,我会尝试一下。我很好奇19是从哪里来的。只是一个随机数,选择19,因为19行在你的屏幕上是可见的啊,有道理。谢谢你。我会在可能的时候试一试。我很好奇19是从哪里来的。只是一个随机数,选择19,因为19行在你的屏幕上是可见的啊,有道理。谢谢,谢谢,有机会我会试试的。谢谢,有机会我会试试的。