Двойные строковые функции VBA - PullRequest
1 голос
/ 24 марта 2020

Это, вероятно, очень основа c для большинства здесь, у меня нет предыстории и просто нужна помощь. Я должен написать функцию isDup в VBA, чтобы сравнить два твита на основе их одинакового количества слов, чтобы определить, являются ли твиты дублирующимися или нет, на основе выбранного десятичного порога (0-1). Мой процесс состоит в том, чтобы написать сабвуфер с двумя жестко закодированными твитами, которые предоставил мой проф (просто для понимания перед преобразованием в функцию), и я столкнулся с ошибкой времени выполнения 5. Я опубликовал код, который у меня есть. Любая помощь будет принята с благодарностью, спасибо!

Option Explicit
Sub isDup()
    Dim tweet1 As String
    Dim tweet2 As String
    Dim threshold As Double
        threshold = 0.7

    tweet1 = "Hours of planning can save weeks of coding"
    tweet2 = "Weeks of programming can save you hours of planning"

    Dim tweet1Split() As String
        tweet1Split = Split(tweet1, " ")

    Dim tweet2Split() As String
        tweet2Split = Split(tweet2, " ")

    Dim i As Integer
    Dim j As Integer

    Dim sameCount As Integer

    'my thought process below was to compare strings i and j to see if equal, and if true add 1 to sameCount,
    'but the If StrComp line is where the error is

    For i = LBound(tweet1Split) To UBound(tweet1Split) Step 1
        For j = LBound(tweet2Split) To UBound(tweet2Split) Step 1
            If StrComp(i, j, vbDatabaseCompare) = 0 Then
                sameCount = sameCount + 1
                Exit For
            End If
        Next j
    Next i
End Sub
   'here i wanted to get a total count of the first tweet to compare, the duplicate tweet is true based on the number of
   'similar words
    Function totalWords(tweet1 As String) As Integer
            totalWords = 0
        Dim stringLength As Integer
        Dim currentCharacter As Integer

            stringLength = Len(tweet1)

        For currentCharacter = 1 To stringLength

        If (Mid(tweet1, currentCharacter, 1)) = " " Then
            totalWords = totalWords + 1
        End If

        Next currentCharacter
    End Function

    'this is where i compute an "isDup score" based on similar words compared to total words in tweet1, in this
    'example the threshold was stated above at 0.7
    Dim score As Double
        score = sameCount / totalWords

    If score > threshold Then
        MsgBox "isDup Score: " & score & " ...This is a duplicate"
    Else
        MsgBox "isDup Score: " & score & " ...This is not a duplicate"
    End If

End Sub

1 Ответ

1 голос
/ 24 марта 2020

Первый выпуск:

i и j - это просто индексы. Вы хотите сравнить строку, к которой относится ваш индекс, так:

If StrComp(tweet1Split(i), tweet2Split(j), vbDatabaseCompare) = 0 Then

Второй выпуск:

Как отмечено в документации Microsoft для StrComp , vbDatabaseCompare зарезервирован для доступа, который вы не используете, следовательно, источник вашей второй ошибки. Вам нужно перейти на другое сравнение

...