1

Thanks for reading this.

I'm working on a personal project. I wrote a script that adds Scripting Runtime (to use Dictionary type), then declares a variable such as DictSummary as Scripting.Dictionary. The problem is, VBA does not allow this as it scans the code for error even before running it. When the Scripting Runtime is not added, the Dim DictSummary as Scripting.Dictrionary yields "User-defined type not defined" error.

If ReferenceIsAdded("Scripting") = False Then
    If ProjectIsNA Then
        Exit Function
    Else
        ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\System32\scrrun.dll"
    End If
Dim DictSummary As Scripting.Dictionary

I tried adding a Function that declares, assigns, and returns a variable of Dictionary type, to avoid this error.

Public Function AddADict() As Variant
    Dim myDict As Scripting.Dictionary: Set myDict = New Scripting.Dictionary
    Set AddADict = myDict
End Function

Then I get the variable like this

Set DictSummary = AddAdict()

It does avoid that error but the returned variable is not quite like that, the type of DictSummary is Variant/Object/Dictionary, while it should be Dictionary/Dictionary for that variable to work. I tried On Error Resume Next but it does not help.

Is there a way to avoid that, or is there a way to optionally declare a variable?

4
  • If it is a personal project, why not just add the reference manually? It takes about 10 seconds. If it isn't a personal project, just use late binding Dim myDict As Object: Set myDict = CreateObject("Scripting.Dictionary"). VBA does have some support for conditional compilation, but I don't think that it would help you here. Commented Jan 27, 2019 at 13:56
  • How does Variant/Object/Dictionary not work? Commented Jan 27, 2019 at 15:00
  • @SMeaden Functionally it would work fine -- but it wouldn't hook into Intellisence (which I am guessing makes OP unhappy with this somewhat odd way of late binding). Commented Jan 27, 2019 at 15:05
  • @JohnColeman thanks for the late binding tips, I read it somewhere but don't have sense that it would work here. Also the Variant/Object/Dictionary works fine, I just made stupid code so it appears "not working" Commented Jan 27, 2019 at 16:09

1 Answer 1

1

Do you protest at the Variant because you could define your function to return an Object. Running the sub TestAddDict below the variable objDict can be seen in the Locals Window to be of type Object/Dictionary

Option Explicit

Function AddADict() As Object
    Set AddADict = VBA.CreateObject("Scripting.Dictionary")
End Function

Sub TestAddADict()
    Dim objDict As Object
    Set objDict = AddADict
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Holly**** I didn't even think about returning an object, I'm pretty new to this. Thanks anyway, both Variant and Object works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.