简体   繁体   中英

VBA Excel macro can't create SortedList

I have a VBA macro that runs in Excel and uses a SortedList. On most machines it runs fine. On one, the line

Dim myEvents As New SortedList

results in myEvents having a value of Nothing. The line

myEvents = createobject("system.collections.sortedlist")

causes an "Automation Error". mscorlib.dll is in my References and is checked.

Anybody have any idea what's going on? Since the macro works on other machines I think it must be some system setup problem, but I don't know what it could be.

OS Name Microsoft Windows 10 Pro
Version 10.0.18362 Build 18362
System Model    Surface Laptop
System Type x64-based PC
Dim myEvents As New SortedList

For an early-bound, auto-instantiated declaration to result in myEvents being Nothing , things need to be very, very, very broken.

Because the minute an auto-instantiated object is created, you can never test it for Nothing and get True - this assertion can never be met, because the mere referencing of myEvents in the expression is enough for VBA to go and re-instantiate the object before it evaluates Is Nothing :

Debug.Assert myEvents Is Nothing

You can only declare something As SortedList with a project reference to the type library that defines this class.

 myEvents = createobject("system.collections.sortedlist")

Two problems here: one, the Set keyword is missing. We're assigning an object reference, Set is required. Next is the use of CreateObject to make a New instance of a type that's defined in a library that's already loaded and referenced: the compiler already knows where to find the type, but you're making it hit the Windows Registry to find out regardless. New it up instead, and keep CreateObject for things defined in libraries that are NOT referenced.

Set myEvents = New SortedList

That should be all you need, assuming the reference is correct.

mscorlib.dll is in my References and is checked.

Not the DLL, but the TLB. And it has to be of the correct bitness for the computer that's going to run it, and the correct bitness is going to have to match the bitness of the host application, ie Excel.

You can't early-bind to C:\\Windows\\Microsoft.NET\\Framework64\\ and have it work in 32-bit Excel. And you can't early-bind to C:\\Windows\\Microsoft.NET\\Framework\\ and have it work in 64-bit Excel.

If some of your users are using a different bitness install of Office, either fix their install to match everyone else's, or remove the reference and use late binding:

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

On the down side you'll be coding in the dark, without compiler assistance and with the documentation nearby.

On the up side, the late-bound code should work on all Windows systems.

I had a similar problem and it turned out the problem was the reference before mscorlib wasn't found, in this case the Tools > Reference page had "MISSING: Microsoft Excel 16.0 Object Library". I solved it by unticking the missing library, selecting ok then re-adding the mscorlib reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM