I have created a DLL file in VB.NET and I want use it in Excel VBA. When I use it like a function it is working perfect but when I use sub with a ByRef variable it does not work and Excel restarts with an error.
The code in VB.NET is:
Public Function distinctArr(ByVal arr As String()) As String()
Return arr.ToList.Distinct.ToArray
End Function
Public Sub sortArr(ByVal arr As String(), ByRef a As String())
Dim tolist As List(Of String) = arr.ToList
tolist.Sort()
a = tolist.ToArray
End Sub
This is the code in VBA:
Dim objMda As Excelcode.mda
Set objMda = New Excelcode.mda
Dim distinc_Item() As String
Dim all_Items() As String
all_Items = rng_to_string(rng_rizmetre)
distinc_Item = objMda.distinctArr(all_Items) '''This line is working perfect
Dim Sorted_Item() As String
objMda.sortArr distinc_Item, Sorted_Item
What is wrong with the code?
objMdadeclared and assigned? Does the VB.NET code have the required type and member attributes? Do you see your VB.NET API in VBA's object browser? Have you tried passing the arraysAs ObjectandByRef? Is your VB.NET code catching any exception? Have you tried debugging the VB.NET code by attaching to the EXCEL.EXE process? Do you know that you could useSystem.Collections.ArrayListdirectly in VBA for this?Functioninstead of returning your result with aByRefparameter.Return arr.Distinct.ToArrayanda = arr.ToArray : Array.Sort(a)