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?
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.Variant/Object/Dictionarynot work?