title: User defined types in arrays/collections and for each loops
body: <p>VBA shows in a popup that i am not allowed to iterate through an array with user defined types.
  I wrote a bit of code and wonder how i can work around this.
  Here is a mini example that focusses on what i want to be able to do.</p>
  
  <pre><code>Option Explicit
  
  Type Info
      source As String
      destination As String
  End Type
  
  Sub specialCopy()
      Dim target As Variant
      Dim AllTargets() As Info: AllTargets = SetAllTargets()
      For Each target In AllTargets
          CopyValues (target)
      Next
  End Sub
  
  Function SetAllTargets() As Info()
      Dim A As Info: A = SetInfo("A1", "B1")
      Dim B As Info: B = SetInfo("A2", "B2")
      Dim AllTargets() As Info
      Set AllTargets = Array(A, B)
  End Function
  
  Function SetInfo(source As String, target As String) As Info
      SetInfo.source = source
      SetInfo.destination = destination
  End Function
  
  Sub CopyValues(target As Info)
      Range(target.source).Select
      Selection.Copy
      Range(target.destination).Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  End Sub
  </code></pre>
  
  <p>How can i iterate through my <code>AllTargets</code> array?
  Since i am unable to compile this there may be more than one problem here.
  I am not entirely sure if the way i set up the <code>AllTargets</code> list is a valid syntax.</p>
  
  <hr>
  
  <p>I reworked the example to narrow down the problems in the code:</p>
  
  <pre><code>Option Explicit
  
  Type Info
      source As String
      destination As String
  End Type
  
  Sub specialCopy()
      Dim target As Variant
      Dim AllTargets As Collection: Set AllTargets = SetAllTargets()
      For Each target In AllTargets
          CopyValues (target) '2. unkown if this is possible
      Next
  End Sub
  
  Function SetAllTargets() As Collection
      Dim A As Info: A = SetInfo("A1", "B1")
      Dim B As Info: B = SetInfo("A2", "B2")
      Set SetAllTargets = New Collection
      SetAllTargets.Add (A) '1. problem here when assigning user type
      SetAllTargets.Add (B) '1. problem here when assigning user type
  End Function
  
  Function SetInfo(source As String, destination As String) As Info
      SetInfo.source = source
      SetInfo.destination = destination
  End Function
  
  Sub CopyValues(target As Info)
      Range(target.source).Select
      Selection.Copy
      Range(target.destination).Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
          :=False, Transpose:=False
  End Sub
  </code></pre>
  
  <p>The code went from Array to Collection - never the less there are still issues in it that i can not solve now.</p>
  
  <p>I think the root cause stayed the same: using user defined types.
  I marked as a comment where i think the problems are located.</p>