Arrays 检测tweet是否相似的函数

Arrays 检测tweet是否相似的函数,arrays,excel,vba,nested-loops,Arrays,Excel,Vba,Nested Loops,我试图在VBA中创建一个函数,该函数接受2个字符串和一个阈值(十进制形式的百分比),如果字符串包含的相同单词的百分比高于阈值,则返回true。 这是我到目前为止的代码 Function isDup(tweet1 As String, tweet2 As String, threshold As Double) As Boolean 'Declare variables to store words from each tweet Dim C1 As String Dim

我试图在VBA中创建一个函数,该函数接受2个字符串和一个阈值(十进制形式的百分比),如果字符串包含的相同单词的百分比高于阈值,则返回true。 这是我到目前为止的代码

Function isDup(tweet1 As String, tweet2 As String, threshold As Double) As Boolean
    'Declare variables to store words from each tweet
    Dim C1 As String
    Dim C2 As String

    'Use split function to split each tweet into single words. The " " is the delimiter, each space creates a new word
    C1 = Split(tweet1, " ")
    C2 = Split(tweet2, " ")

    'Loop through each word from tweet1 and each word from tweet2
    For i = LBound(C1) To UBound(C1)
        For j = LBound(C2) To UBound(C2)
            'Declare variable to store result from StrComp Function
            Dim Cresult As Double

            'Use StrComp Function to compare the current word from tweet1 to the current word from tweet2
            Cresult = StrComp(i, j, vbTextCompare)
        Next i
    Next j

    'Use If Then to return true if the tweets are more similar than the percentage given by the threshold
    If Cresult > threshold Then
    isDup = True

End Function

我是VBA新手,所以有一些错误,特别是我一直遇到预期的:数组错误。任何帮助都将不胜感激,谢谢

这里有一个快速重写,修复了我在上面的评论中提到的东西。如果这不完全是你想要的,那你就应该有个大概的答案

Function isDup(tweet1 As String, tweet2 As String, threshold As Double) As Boolean
    'Declare variables to store words from each tweet
    Dim C1 As Variant
    Dim C2 As Variant

    'Use split function to split each tweet into single words. The " " is the delimiter, each space creates a new word
    C1 = Split(tweet1, " ")
    C2 = Split(tweet2, " ")

    'Declare variable to store result from StrComp Function
    Dim Cresult As Double

    'Loop through each word from tweet1 and each word from tweet2
    For i = LBound(C1) To UBound(C1)
        For j = LBound(C2) To UBound(C2)

            'Use StrComp Function to compare the current word from tweet1 to the current word from tweet2
            If StrComp(C1(i), C2(j), vbTextCompare) = 0 Then
                Cresult = Cresult + 1
            End If
        Next j
    Next i

    'Use If Then to return true if the tweets are more similar than the percentage given by the threshold
    If Cresult > threshold Then
        isDup = True
    End If

End Function

循环中变量
Cresult
中存储的值在每次迭代时都会重置。因此,当您最终比较
If Cresult>阈值时,它只是比较循环中的最后一次迭代。将数组声明为
Variant
,而不是
String
。在迭代内部
j
循环之前,也要迭代
i
循环,这样,除非修复,否则也会抛出错误。StrComp
正在比较循环变量
i
j
,它们只是整数。相反,您希望比较存储在数组中循环变量位置处的值,如
StrComp(C1(i)、C2(j)、vbtextcomper)
您昨天没有问过类似的问题吗?(如果我没记错的话,还可以从Excel Hero那里得到答案)@JNevill-据我所知,Dim语句没有执行。。。所以它“重新申报”不是真的