gpt4 book ai didi

vba - VBA 中的通配符字典搜索

转载 作者:行者123 更新时间:2023-12-02 20:06:02 24 4
gpt4 key购买 nike

是否可以根据 Excel 中内置的拼写检查词典搜索特定的单词模式(即包含通配符的模式)?

Debug.Print Application.CheckSpelling("hel*")

例如,如果我搜索 "hel*",我希望上面的代码返回 True,因为某些实际单词确实与此模式匹配,例如“你好”、“ hell ”等。

但是,这并没有按预期工作:它返回False

最佳答案

写这个有点有趣...它适用于单字符通配符,即 ?。它不适用于多字符通配符 (*),但我仍然认为这是一条有趣的前进道路。

警告:这使用了递归,执行时间随着输入字符串中 ? 的数量呈指数增长!

Function CheckSpellingWithWildcards(ByVal s As String)
Const wildcardChar As String = "?"
Dim i As Integer
Dim firstWildcardPos As Long
firstWildcardPos = InStr(s, wildcardChar) 'Find first wildcard
If firstWildcardPos = 0 Then
'No wildcards left — look it up in the dictionary.
CheckSpellingWithWildcards = Application.CheckSpelling(s)
Else
CheckSpellingWithWildcards = False
For i = 97 To 122 'a to z. Adjust if you need e.g. çæøåéëï as well
Mid(s, firstWildcardPos, 1) = Chr(i) 'Replace wildcard with letter
If CheckSpellingWithWildcards(s) Then 'note: recursion!
'Found a match! Get out.
CheckSpellingWithWildcards = True
Exit Function
End If
Next i
End If
End Function

使用示例:

?CheckSpellingWithWildcards("Hel?")
True
?CheckSpellingWithWildcards("Hel?o")
True
?CheckSpellingWithWildcards("Hel?p")
False
?CheckSpellingWithWildcards("Comm?nica?ion")
True
?CheckSpellingWithWildcards("C?mm?nyca?ion") '30 seconds later...
False

关于vba - VBA 中的通配符字典搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com