Pass user-defined type to Function

Lammy

New Member
Joined
Dec 14, 2014
Messages
22
Dear all,

I am new to VBA and I am struggling getting the syntax right.

What I am trying to achieve:
- Read a Range from a Sheet
- Paste it in a different Sheet

Limitations are, that the Range and hence the Array store different types, hence the user-defined type ("MyArrayType").

Here is what I tried so far.

VBA Code:
Private Type MyArrayType
    ProjectId As Integer
    Location As String
End Type

Public Sub MySubRoutine()

    Dim MyArrayRecords(30) As MyArrayType
   
    ' Read Values from Sheet to Array
    For i = 2 To 30
        With MyArrayRecords(i)
            .ProjectId = Cells(i, 2).Value
            .Location = Cells(i, 1).Value
        End With
    Next i

    ' Paste Array to Sheet
    ProcessArray (MyArrayRecords)
End Sub

Function ProcessArray(MyArrayRecords As MyArrayType) As String

    With ActiveSheet
        .Range("H10").Resize(UBound(MyArrayRecords, 1)).Value = MyArrayRecords
    End With
   
End Function

I can get the code to work if I use a normal Array(), but not with my user-define type. It returns "ByRef argument type mismatch".

Thank you for your insights.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You haven't declared the function to take an array of the type - it should be:

Code:
Function ProcessArray(MyArrayRecords() As MyArrayType) As String
 
Upvote 0
Actually, I had the same idea, but when adjusting I still receive
"Type mismatch: array or user-defined type expected"

Unbenannt.PNG
 
Upvote 0
@Answering because edit-timeout

I think the mistake was actually that I did not use "Call" to execute the function. At least the error message changes:

1636121893047.png
 
Last edited:
Upvote 0
I'd suggest you switch to a class. UDTs are a little limited in VBA.
 
Upvote 0

Forum statistics

Threads
1,215,484
Messages
6,125,066
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top