VBA - 字典按值调用键?

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

似乎大多数答案都涉及C#所以我觉得相对安全的问:

我需要通过它的值来调用字典键。我有一个包含四个字符键列表的字典对象。例如:

dSCAC.Add "AAAA", 1
dSCAC.Add "BBBB", 2
dSCAC.Add "CCCC", 3
dSCAC.Add "DDDD", 4

等等

我已经能够在字符串中找到与键相关联的值(在本例中为电子邮件主题行),然后在值中添加或删除1。

例如:检测到BBBB,值为2.修改该值并调用相应的密钥。在这种情况下,最后我们想要varOtherSCAC = AAAA。

If dSCAC(varSCAC) Mod 2 Then
        Debug.Print "Odd " & "PAPS"
        varOtherSCAC = (dSCAC(varSCAC) + 1)            
        Debug.Print "Opposite SCAC is " & varOtherSCAC

        Else
        Debug.Print "Even " & " PARS"
        varOtherSCAC = (dSCAC(varSCAC) - 1)
        Debug.Print "Opposite SCAC is " & varOtherSCAC
End if

我无法根据此值计算出调用新密钥的语法。它甚至可以在VBA中使用吗?有没有解决的办法?

vba outlook outlook-vba
2个回答
1
投票

我会对@ TateGarringer采取类似的方法,但是将两个Dictionary对象包装在一个类中,为它们提供一个通用接口,使事情更容易使用:

'In a class module named MirroredDictionary.cls (add reference to Scripting Runtime)
Option Explicit

Private backing As Scripting.Dictionary
Private mirror As Scripting.Dictionary

Private Sub Class_Initialize()
    Set backing = New Scripting.Dictionary
    Set mirror = New Scripting.Dictionary
End Sub

Public Sub Add(Key As Variant, Value As Variant)
    backing.Add Key, Value
    mirror.Add Value, Key
End Sub

Public Function KeyExists(Key As Variant) As Boolean
    KeyExists = backing.Exists(Key)
End Function

Public Function ValueExists(Value As Variant) As Boolean
    ValueExists = mirror.Exists(Value)
End Function

Public Function ValueFromKey(Key As Variant) As Variant
    ValueFromKey = backing.Item(Key)
End Function

Public Function KeyFromValue(Value As Variant) As Variant
    KeyFromValue = mirror.Item(Value)
End Function

根据您打算如何使用它,您可能想要也可能不想包装其他功能。用法类似于普通的Dictionary(除了属性名称的一些差异,尽管你可以改变它们的味道):

Public Sub Example()
    Set sample = New MirroredDictionary

    sample.Add "AAAA", 1
    sample.Add "BBBB", 2
    sample.Add "CCCC", 3
    sample.Add "DDDD", 4

    Debug.Print sample.ValueFromKey("AAAA")     '1
    Debug.Print sample.KeyFromValue(1)          'AAAA
    Debug.Print sample.ValueFromKey("BBBB")     '2
    Debug.Print sample.KeyFromValue(2)          'BBBB
    Debug.Print sample.ValueFromKey("CCCC")     '3
    Debug.Print sample.KeyFromValue(3)          'CCCC
    Debug.Print sample.ValueFromKey("DDDD")     '4
    Debug.Print sample.KeyFromValue(4)          'DDDD
End Sub

0
投票

你总是可以创建一个额外的字典来存储你的Key-Value对与你的Value作为你的key和你的key作为value

Sub test()
Dim dSCAC As Object
Dim dSCACArr As Object
Dim varOtherSCAC As String
Dim key
Dim varSCAC


Set dSCAC = CreateObject("Scripting.Dictionary")
Set dSCACArr = CreateObject("Scripting.Dictionary")

dSCAC.Add "AAAA", 1
dSCAC.Add "BBBB", 2
dSCAC.Add "CCCC", 3
dSCAC.Add "DDDD", 4

For Each key In dSCAC.Keys
    dSCACArr.Add dSCAC(key), key
Next

For Each varSCAC In dSCAC.Keys
    If dSCAC(varSCAC) Mod 2 Then
            Debug.Print "Odd " & "PAPS"
            varOtherSCAC = dSCACArr(dSCAC(varSCAC) + 1)
            Debug.Print "Opposite SCAC is " & varOtherSCAC

            Else
            Debug.Print "Even " & " PARS"
            varOtherSCAC = dSCACArr(dSCAC(varSCAC) - 1)
            Debug.Print "Opposite SCAC is " & varOtherSCAC
    End If
Next

End Sub

这产生了结果

Odd PAPS
Opposite SCAC is BBBB
Even  PARS
Opposite SCAC is AAAA
Odd PAPS
Opposite SCAC is DDDD
Even  PARS
Opposite SCAC is CCCC

编辑:

For Each varSCAC In dSCAC.Keys...Next仅用于概念验证。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.