VBA - Countif范围匹配数组

时间:2017-11-28 15:56:28

标签: arrays excel vba excel-vba match

我不知道如何完成这项任务。如果该范围中单元格中的至少一个单词与数组匹配,我需要计算工作表中的范围。例如,如果单元格"B2"有一个带有数组中某个单词的句子,则计为1,如果它与数组匹配,则只计算countif范围。我的代码会更好地显示我的问题,所以如果这一点令人困惑,我会道歉。

With ThisWorkbook
    Dim Keywords As Variant
    Dim iVal As Double
    keyword = Array("*cold*", "*hot*", "*warm*", "*cool*", _
        "*temp*", "*thermostat*", "*heat*", "*temperature*", _
        "*not working*", "*see above*", "*broken*", "*freezing*", _
        "*warmer*", "*air conditioning*", "*humidity*", _
        "*humid*")
    iVal=Application.WorksheetFunction.CountIf(Range("B2",Range("B2").End(xlDown)),keyword)

    Dim rep As Worksheet
    Set rep = Worksheets("Report")
    rep.Range("A1") = iVal
End With

正如我所示,如果数组中的其中一个单词在定义的范围{@ 1}}中的单元格中匹配,则计算并显示Range("B2", Range("B2").End(xlDown))中的值。感谢所有的帮助,谢谢。

2 个答案:

答案 0 :(得分:3)

您需要比较彼此之间值的两个列表 - 您的RangeArray。最简单的方法是通过嵌套循环。像这样:

Sub TestMe()

    With ThisWorkbook

        Dim Keywords    As Variant
        Dim iVal        As Long
        Dim myRange     As Range
        Dim myCell      As Range
        Dim bCount      As Boolean
        Dim myVal       As Variant

        keyword = Array("cold", "hot", "warm", "cool", _
                        "temp", "thermostat", "heat", "temperature", _
                        "not working", "see above", "broken", "freezing", _
                        "warmer", "air conditioning", "humidity", _
                        "humid")

        Set myRange = Columns(2).SpecialCells(2)

        For Each myCell In myRange
            bCount = False

            For Each myVal In keyword
                If InStr(1, myCell, myVal, vbTextCompare) Then bCount = True
            Next myVal

            If bCount Then iVal = iVal + 1                
        Next myCell

        Debug.Print iVal
    End With

End Sub

只要标志bCount设置为True,就会完成计数。它通过外循环的每次迭代重置为False。 我会移除*,就我使用InStr()进行检查而言,星星在那里有点无用。

此外Columns(2).SpecialCells(2)返回一个范围,仅包含值,在列B中为非公式且非空。

答案 1 :(得分:1)

这是我和@Vityata的答案。请注意,在将数组读入数组时,可以使用他对SpecialCells的建议来忽略空格。

注意它会查找"*Cold*",如果它是数组中的内容。简单地将"Cold"等放在数组中,如果这是在单元格中查找的单词。

Option Explicit

Public Sub test()

    Dim Keywords As Variant
    Dim iVal As Long
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.Sheets(2)

    Keywords = Array("*cold*", "*hot*", "*warm*", "*cool*", _
                     "*temp*", "*thermostat*", "*heat*", "*temperature*", _
                     "*not working*", "*see above*", "*broken*", "*freezing*", _
                     "*warmer*", "*air conditioning*", "*humidity*", _
                     "*humid*")
    With ws

        Dim rangetoCheck()
        Dim counter1 As Long
        Dim counter2 As Long
        Dim totalCount As Long
        iVal = 0
        rangetoCheck = .Range("B2", .Range("B2").End(xlDown)).value

        For counter1 = LBound(rangetoCheck, 1) To UBound(rangetoCheck, 1)

            For counter2 = LBound(Keywords) To UBound(Keywords)

                If InStr(1, rangetoCheck(counter1, 1), Keywords(counter2), vbBinaryCompare) Then
                    iVal = iVal + 1
                    Exit For
                End If

            Next counter2

        Next counter1

    End With

    MsgBox iVal

End Sub