Dictionary doesn't display Items for certain Key (Numeric value)

前端 未结 1 1396
轮回少年
轮回少年 2021-01-23 04:26

This is a Long one, but stay with me...

I have a Dictionary that saves \"PO\" as Key and \"SO\" as Items (there can be cases t

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-23 05:03

    The numeric keys were entered as numbers and you are fetching them as strings. I suggest that you stick to one convention for your dictionary.

    Sub TestDict()
      Dim dict As New Dictionary
      dict.Add 1, "one"
      dict.Add "2", "two"
    
      Debug.Print dict("1")     ' Nothing
      Debug.Print dict(1)       ' one
    
      Debug.Print dict("2")    ' two
      Debug.Print dict(2)      ' Nothing
    End Sub
    

    Solution

    Chose a convention for your dictionary and stick to it. In this application I would take the convention of always converting my keys to strings, both when inserting and when fetching. A few changes in your code can achieve it:

    If Not PODict.Exists(CStr(Range("A" & i).Value) Then ' could use .Text also
    
    PODict.Add CStr(ID), Names
    
    
    SOArr = Split(PODict(CStr(POSelected)), ",") ' maybe not needed here, but to illustrate
    

    0 讨论(0)
提交回复
热议问题