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

南楼画角 提交于 2020-05-17 08:47:25

问题


I'm working on a project to develop features to train a machine learning classifier with a set of text reviews. The feature I'm having trouble with is to count if a review has the specific keyword "amazing" in it, and then display a 1 or a 0 in a column next to the review depending if the keyword was found or not.

The main issue being that my values wont display in the column. This is the only coding/programming course I have taken and I am a beginner.

'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

回答1:


IIUC, perhaps a simpler way to do this:

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

Change SEARCH to FIND if you want this to be case-sensitive.

For a search on two words, perhaps:

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

Change the OR to AND if you want both to be found.



来源:https://stackoverflow.com/questions/61508901/how-to-call-a-subroutine-to-multiple-rows-in-a-column-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!