字典中部分存在 - VBA

问题描述 投票:0回答:1

我正在寻找一种方法来检查字典、Excel - VBA 中是否包含某个值。然而,在这种情况下,简单的

dict.Exists("")
并不是正确的方法,因为搜索值与字典中的值不完全相同。

例如,我在字典中查找单词

apple
,其中包含短语
fruit apple
。所以
apple
在字典中包含为
fruit apple
。尽量避免区分大小写。

目前,我循环字典,这很耗时。

还有类似的补充吗

dict.Exists("*apple*")

有什么想法吗?

excel vba dictionary
1个回答
0
投票

Dictionary.Keys
方法将返回一个包含所有键的数组。

Excel 的

MATCH
函数 允许您搜索数组(在 VBA 中可以称为 `Application.Match)。 这支持通配符,并且区分大小写——但是,这并不是说它只返回一个单个结果,即使字典中的多个键与模式匹配。

两者结合:

Dim vArr AS Variant, vMatch As Variant
vArr = dict.keys
vMatch = Application.Match("*apple*", vArr, 0)
If IsError(vMatch) Then
    MsgBox "Key Not Found", vbCritical
Else
    MsgBox vArr(vMatch-1), vbInformation 'the -1 is because the Array is Zero-Indexed, but Match returns One-Indexed results
End If
© www.soinside.com 2019 - 2024. All rights reserved.