title: Acces an item in a dictionary with a custom object key in vba
body: <p>I've got a problem accessing items in a dictionary in vba.</p>
  
  <p>I've the following dictionary:</p>
  
  <pre><code>Dim CResults as dictionary
  </code></pre>
  
  <p>I add items:</p>
  
  <pre><code>CResults.add currentkey, result
  </code></pre>
  
  <p><code>Currentkey</code> is an object that I created myself from a class called <code>DCRkey</code></p>
  
  <pre><code>Private loadcase as long
  Private csystem as String
  Private node as long
  </code></pre>
  
  <p>and <code>result</code> is another object from a class called <code>DCR</code>:</p>
  
  <pre><code>Private Fs as double
  Private C1 as double
  Private C2 as double
  ...
  </code></pre>
  
  <p>Then I try to access an item using  </p>
  
  <pre><code>Dim accesskey as DCRKey
  accesskey.loadcase=10
  accesskey.node = 2000
  accesskey.csystem="Global"
  Sheets("Results").cells(i,1).value= CResults(accesskey).C1
  </code></pre>
  
  <p>That is where I've got the error: <code>runtime error 424 object required</code></p>
  
  <p>Then I thought maybe the key and the item that I search where not imported, so I decided to display my entire dictionary on an excel worksheet:</p>
  
  <pre><code>Dim testkey as variant
  dim i as integer
  i=1
  with worksheet("Test")
      For each testkey in CResults.keys
          .cells(i,1)=test.node
          .cells(i,2)=test.loadcase
          .cells(i,3)=test.csystem
          .cells(i,4)=Cresults(testkey).C1
          .cells(i,5)=Cresults(testkey).Fs
          if accesskey.loadcase=testkey.loadcase and accesskey.node=testkey.node and accesskey.csystem=testkey.csystem then
              Msgbox "This key is the same as the accesskey"
          End if
          i=i+1
      Next
  End with
  </code></pre>
  
  <p>What I see is:</p>
  
  <ol>
  <li>The key that I was searching earlier exists in the dictionary : visual check on the sheet</li>
  <li>The key that I was searching earlier realy exists in the dictionary : <code>"This key is the same as the acceskey"</code> is displayed once</li>
  <li>Accessing an item in the dictionary from the <code>for each</code> loop works because C1 and Fs are displayed correctly on the worksheet</li>
  </ol>
  
  <p>Then I thought maybe it is due to the fact that <code>testkey</code> is defined as a variant and not a <code>DCRKey</code>, so I tried that:</p>
  
  <pre><code>dim a as variant
  Set a = currentkey
  .Cells(i,1) = CResults(a).C1
  </code></pre>
  
  <p>But it doesn't works and I still got the <code>runtime error 424</code>.</p>
  
  <p>I also did is try:</p>
  
  <pre><code>CResults.exists(accesskey)
  </code></pre>
  
  <p>It returns false and create a new entry in the dictionary (btw I hate when it does that), with the same key as acceskey and an empty item.</p>
  
  <p>So my question is: why accessing an item using a custom type key works in a <code>for each</code> loop and not in a standalone call. What have I missed? This code is very similar to the code that I wrote but not exactly the same (for you to understand better). Tell me if you think that the real code could help.
  Thanks for any help.</p>