尝试编写一个 VBA 函数来比较两个单词中的字母,看看它们是否有共同的字母

Trying to write a VBA function that compares the letters in two words to see if they have any letters in common

函数 wordPass 应该以两个 words/strings 作为参数并查找它们共有的字母。如果他们有共同的字母,函数 returns True,如果没有,它 returns False.

这是我尝试过的方法,但它不起作用:

Function wordPass(wordleAnswer As String, guess As String) As Boolean

  For i = 1 To Len(wordleAnswer)
    For j = 1 To Len(guess)
      same = False
      If Mid(guess, j, 1) = Mid(wordleAnswer, i, 1) Then
        same = True
      End If
    Next j
  Next i

  wordPass = same

End Function

我们可以使用InStr消除一个循环:

Function wordPass(wordleAnswer As String, guess As String) As Boolean
    Dim i As Long
    For i = 1 To Len(guess)
        If InStr(wordleAnswer, Mid$(guess, i, 1)) > 0 Then
            wordPass = True
            Exit Function
        End If
    Next
End Function