是否可以根据Excel内置的拼写检查字典搜索特定的单词模式(即包含通配符的模式)?
Debug.Print Application.CheckSpelling("hel*")
例如,True
如果要搜索,我希望上面的代码返回"hel*"
,因为某些实际单词确实匹配此模式,例如“ hello”,“ 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句