VBA中的通配符字典搜索

卡尔提克·莫汉(Karthik Mohan)

是否可以根据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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

字典的通配符搜索

来自分类Dev

VBA使用通配符进行搜索

来自分类Dev

数组中的通配符搜索

来自分类Dev

在具有多个通配符值的“字典”文件中搜索字符串

来自分类Dev

使用通配符在notepad ++中搜索

来自分类Dev

全文搜索的ContainsTable中的通配符

来自分类Dev

用通配符在postgresql中搜索

来自分类Dev

使用通配符在notepad ++中搜索

来自分类Dev

PYMYSQL 搜索中的 SQL 通配符

来自分类Dev

Teradata 中的 LIKE 通配符搜索

来自分类Dev

MySQL全文搜索中的通配符搜索

来自分类Dev

无法在字典中搜索

来自分类Dev

在列表中搜索字典

来自分类Dev

在字典中搜索值

来自分类Dev

在字典中搜索

来自分类Dev

通配符搜索在Kibana中不起作用

来自分类Dev

memberOf中的LDAP搜索通配符

来自分类Dev

Primefaces Datatable列过滤中的通配符搜索?

来自分类Dev

使用通配符SQL在TextField中搜索单词

来自分类Dev

IN运算符中的通配符搜索

来自分类Dev

通配符搜索在Kibana中不起作用

来自分类Dev

在ElasticSearch中对嵌套对象进行通配符搜索

来自分类Dev

Notepad ++搜索和替换中的通配符问题

来自分类Dev

在本地商店中搜索通配符证书?

来自分类Dev

使用通配符在许多表php中搜索

来自分类Dev

Primefaces Datatable列过滤中的通配符搜索?

来自分类Dev

MS Word在高级搜索中终止通配符

来自分类Dev

特定内容结构中的扩展通配符搜索

来自分类Dev

在Swift中在字典中搜索