MDA
MDA

Reputation: 113

Create DLL file in VB.NET and use in Excel VBA

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?

Upvotes: 2

Views: 4263

Answers (1)

MDA
MDA

Reputation: 113

Finally i can find my answer. code in vb.net

Public Class MainClass
    Sub sortArr(ByVal arr As String(), ByRef sortedarr As String())
        sortedarr = arr
        Array.Sort(sortedarr)

    End Sub
End Class

and code in excel vba:

Sub aaa()
Dim Mycode As excelcode.MainClass
Set Mycode = New excelcode.MainClass
Dim arr(2) As String
arr(0) = "m"
arr(1) = "a"
arr(2) = "d"
Dim sortedArr() As String
ReDim sortedArr(0)
  Mycode.sortArr arr, sortedArr
End Sub

by this code i can pass array byval to vb.net dll then vb.net pass sorted array.

Upvotes: 1

Related Questions