VBA 如何对一列中的多行调用子程序

How to call a subroutine to multiple rows in a column in VBA

我正在开展一个项目,该项目旨在开发通过一组文本评论来训练机器学习分类器的功能。我遇到问题的功能是计算评论中是否包含特定关键字 "amazing",然后根据是否找到关键字在评论旁边的列中显示 1 或 0。

主要问题是我的值不会显示在列中。这是我参加的唯一 coding/programming 课程,我是初学者。

'this feature is used to find the keyword "amazing" in the reviews
Sub FindKeywordAmazing()
    Dim cell As Range
    Dim WordCount As Integer
    Dim Line As Integer
    Dim Count As Integer
        WordCount = 0
        Line = 2
        Count = 0

        'using an If statement to update the word count if the keyword amazing is present
        For Each cell In Range("A2:A1001")
            If InStr(cell.Value, "amazing") > 0 Then
                WordCount = WordCount + 1
            Else
                WordCount = 0
            End If

            Count = WordCount
            Range("C" & Line).Value = Count
            Line = Line + 1
        Next

        'calling the sub with the value of the word count to the C column

End Sub

IIUC,也许更简单的方法:

Range("C2:C1001").Formula = "=--ISNUMBER(SEARCH(""amazing"",A2))"

如果您希望区分大小写,请将 SEARCH 更改为 FIND

对于两个词的搜索,可能是:

Range("C2:C1001").Formula = "=--OR(ISNUMBER(SEARCH({""amazing"",""great""},A2)))"

如果您希望两者都被找到,请将 OR 更改为 AND