问题
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