【问题标题】:Compare strings to identify duplicates比较字符串以识别重复项
【发布时间】:2020-07-05 09:30:16
【问题描述】:

我必须编写一个 isDup 函数来根据它们相似的字数比较两条推文,以确定推文是否重复,基于选择的十进制阈值 (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

【问题讨论】:

  • 错误出现在哪一行?
  • 我运行子程序,得到错误并按下调试,这一行突出显示为错误 If StrComp(i, j, vbDatabaseCompare) = 0 Then
  • i 和 j 是索引,即您正在比较索引,而不是您的字符串

标签: arrays excel vba string duplicates


【解决方案1】:

第一期:

ij 只是索引。您想比较与索引相关的字符串:

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

第二期:

正如StrComp 的 Microsoft 文档中所述,vbDatabaseCompare 是为您未使用的 Access 保留的,因此是您的第二个错误的来源。您需要切换到不同的比较

【讨论】:

  • 啊,好的,谢谢,这更有意义。我更新了代码,但我仍然在同一行出现错误,同样的错误(运行时错误 5)。整个代码可能还有另一个问题
  • 现在的问题是 vbDatabaseCompare。您没有使用访问权限。您需要使用不同的比较选项。请看docs.microsoft.com/en-us/office/vba/language/reference/…
  • 哇,这么愚蠢的小错误,修复了那条线,非常感谢!如果您碰巧注意到其他任何内容,或发现任何其他错误,请告诉我,我们将不胜感激!
  • 谢谢,我只想看看另一个问题,我的教授对我拥有的函数 totalWords 有不同的解决方案,我在网上找到了该代码。他已将此作为模板上传,但我仍然对此感到困惑,“ Dim score As Double If UBound(tweet1Split) > UBound(tweet2Split) Then 'compute the isDup score Else 'compute the isDup score End If”
  • 这是一个完全不同的问题,值得提出一个新问题。 Stack Overflow 有助于解决特定问题。如果允许问题永远发展和变化,就不会有最终被接受的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2018-07-09
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多