计算和突出显示短语中的关键字

时间:2015-09-30 07:55:54

标签: excel vba excel-vba scripting highlight

我有一张带有两列的excel表。第一列是关键短语,第二列是消息。关键短语可能出现在消息列中。我需要知道在消息列中发生了多少次关键短语。请提出一些简单易行的方法。

关键短语是一列,消息是第二列。消息列是1个或多个1个关键短语的组合(串联)。我需要找出每条消息包含多少关键短语。

2 个答案:

答案 0 :(得分:4)

您可能能够使用模块子过程收集有效计数,该过程执行内存数组中的所有数学¹并将计数返回到工作表。

Counts keywords in phrases sample data

我使用了一些标准Lorem Ipsum关键字和短语来创建上述示例数据。

点击 Alt + F11 ,当VBE打开时,立即使用下拉菜单插入►模块( Alt + 中号)。将以下内容粘贴到标题为 Book1 - Module1(Code)之类的新模块代码表中。

Option Explicit

Sub count_strings_inside_strings()
    Dim rw As Long, lr As Long
    Dim k As Long, p As Long, vKEYs As Variant, vPHRASEs As Variant, vCOUNTs As Variant

    ReDim vKEYs(0)
    ReDim vPHRASEs(0)

    With Worksheets("Sheet1")   '<~~ set to the correct worksheet name\
        'populate the vKEYs array
        For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            vKEYs(UBound(vKEYs)) = LCase(.Cells(rw, 1).Value2)
            ReDim Preserve vKEYs(UBound(vKEYs) + 1)
        Next rw
        ReDim Preserve vKEYs(UBound(vKEYs) - 1)

        'populate the vPHRASEs array
        For rw = 2 To .Cells(Rows.Count, 2).End(xlUp).Row
            vPHRASEs(UBound(vPHRASEs)) = LCase(.Cells(rw, 2).Value2)
            ReDim Preserve vPHRASEs(UBound(vPHRASEs) + 1)
        Next rw
        ReDim Preserve vPHRASEs(UBound(vPHRASEs) - 1)
        ReDim vCOUNTs(0 To UBound(vPHRASEs))

        'perform the counts
        For p = LBound(vPHRASEs) To UBound(vPHRASEs)
            For k = LBound(vKEYs) To UBound(vKEYs)
                vCOUNTs(p) = CInt(vCOUNTs(p)) + _
                    (Len(vPHRASEs(p)) - Len(Replace(vPHRASEs(p), vKEYs(k), vbNullString))) / Len(vKEYs(k))
            Next k
        Next p

        'return the counts to the worksheet
        .Cells(2, 3).Resize(UBound(vCOUNTs) + 1, 1) = Application.Transpose(vCOUNTs)

        'run the helper procedure to Blue|Bold all of the found keywords within the phrases
        Call key_in_phrase_helper(vKEYs, .Range(.Cells(2, 2), .Cells(Rows.Count, 2).End(xlUp)))

    End With
End Sub

Sub key_in_phrase_helper(vKYs As Variant, rPHRSs As Range)
    Dim p As Long, r As Long, v As Long

    With rPHRSs
        For r = 1 To rPHRSs.Rows.Count
            .Cells(r, 1) = .Cells(r, 1).Value2
            For v = LBound(vKYs) To UBound(vKYs)
                p = 0
                Do While CBool(InStr(p + 1, .Cells(r, 1).Value2, vKYs(v), vbTextCompare))
                    p = InStr(p + 1, .Cells(r, 1).Value2, vKYs(v), vbTextCompare)
                    Debug.Print vKYs(v)
                    With .Cells(r, 1).Characters(Start:=p, Length:=Len(vKYs(v))).Font
                        .Bold = True
                        .ColorIndex = 5
                    End With
                Loop
            Next v
        Next r
    End With
End Sub

您可能必须在5 th 代码行中重命名要处理的工作表。我还包括一个帮助程序,用蓝色|粗体字体识别短语中的关键词。如果不需要,请注释掉或删除第一个子过程底部的Call key_in_phrase_helper(...)行。

点击 Alt + Q 返回工作表。点击 Alt + F8 打开对话框,然后运行子过程。如果您的数据类似于我放在一起的样本数据,那么您应该有类似的结果。

Counts keys in phrases

¹这些是一些先进的方法,但我觉得它们也是解决问题的最佳方法。如果您有具体问题,您自己的研究没有充分解释,我会尝试在评论部分解决这些问题。我创建的用于创建此解决方案的示例工作簿可以根据要求提供。

答案 1 :(得分:0)

您可以从第二行开始使用此公式COUNTIF(B:B;"*"&A2&"*")

相关问题