View Single Post
 
Old 08-30-2023, 06:14 AM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Code:
Sub dicItemExists_Test()
Dim i As Long
Dim arList As Object
Dim dicTest As Object
  On Error GoTo lblError
  'arList.contains will tell you if an item exists in the list (not if in an arrary that is part of the list).
  Set arList = CreateObject("System.Collections.Arraylist")
  arList.Add "Apples"
  arList.Add "Peaches"
  arList.Add "Pears"
  MsgBox arList.Contains("Pears")
  'dicTest.Exists will tell you if a "Key" exists in a dictionary
  Set dicTest = CreateObject("Scripting.Dictionary")
  dicTest.Add "Key2", "AAA"
  dicTest.Add "Key3", "BBB"
  dicTest.Add "Key1", "CCC"
  MsgBox dicTest.Exists("Key1")
  'So, you could use"
  Set dicTest = CreateObject("Scripting.Dictionary")
  dicTest.Add "AAA", "AAAA"
  dicTest.Add "BBB", "BBB"
  dicTest.Add "CCC", "CCC"
  MsgBox dicTest.Exists("CCC")
  'AFAIK, the only way to check if a "value" exists is to loop as you are trying to avoid.
lblExit:
  Set dicTest = Nothing
  Set arList = Nothing
  Exit Sub
lblError:
  Debug.Print "Got error: " & Err.Number & ", " & Err.Description
  GoTo lblExit
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote