2

I have the following VBA code (late binding):

Dim myList As Object
Set myList = CreateObject("System.Collections.SortedList")

which I want to replace with this (early binding):

Dim myList As New SortedList

Has anyone succeeded with this? I suppose that A reference must be enabled. But what is the name of that reference?

4

1 回答 1

2

Probably* the easiest way is to add it on openning:

Private Sub Workbook_Open()
   With ThisWorkbook.VBProject.References
       .AddFromFile "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb"
       '.AddFromguid "{BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}", 2, 4
   End With
End Sub

This adds the mscorlib.dll to the references:

enter image description here

And then:

Public Sub TestMe()
    Dim myList   As SortedList
    Dim myList2  As New ArrayList ' as a bonus!
End Sub

From http://www.snb-vba.eu/VBA_Sortedlist_en.html

  • The SortedList is not an element of the regular VBA-library.
  • The SortedList is part of the library System.Collections.
  • You can find the library in \WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb or in a comparable directory
  • In the VBEditor you can make a direct link to this library manually by checking mscorlib.dll in the References (Tools/references...)
于 2018-04-19T12:13:28.907 回答