VBA 从数组创建字典

  1. Function range2dict(keyarr, valarr) As Scripting.Dictionary
  2. Dim mydict As Object
  3. Dim i, key, val
  4. Set mydict = CreateObject("scripting.dictionary")
  5. For i = 1 To UBound(keyarr) - LBound(keyarr) + 1
  6. key = keyarr(i)
  7. val = valarr(i)
  8. 'Debug.Print key, val
  9. If Not mydict.exists(key) Then
  10. mydict.Add key, val
  11. End If
  12. Next
  13. Set range2dict = mydict
  14. End Function

经测试,第一条代码报错:用户定义类型未定义