簡體   English   中英

如何在 VBA 的列中調用子程序到多行

[英]How to call a subroutine to multiple rows in a column in VBA

我正在開發一個項目,以開發功能來訓練具有一組文本評論的機器學習分類器。 我遇到問題的功能是計算評論中是否包含特定關鍵字“驚人”,然后根據是否找到關鍵字在評論旁邊的列中顯示 1 或 0。

主要問題是我的值不會顯示在列中。 這是我上過的唯一一門編碼/編程課程,而且我是初學者。

'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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM