Hi MrExcel Experts,
I created an array of User-defined Data Type (UDT) and now want to pass it to an Excel Sheet range. How can I do it and do it elegantly?
With my code (shorten for ease of reading), I get the compiler error message: "Only user-defined types defined in public object modules can be coerced to and from a variant or passed to late bound functions".
Note: I created a Public data type and array at the top of a normal module. Also, I did not create a function.
Thank you for any help or suggests.
Kurt
I created an array of User-defined Data Type (UDT) and now want to pass it to an Excel Sheet range. How can I do it and do it elegantly?
With my code (shorten for ease of reading), I get the compiler error message: "Only user-defined types defined in public object modules can be coerced to and from a variant or passed to late bound functions".
Note: I created a Public data type and array at the top of a normal module. Also, I did not create a function.
Thank you for any help or suggests.
Kurt
Code:
'Create custom array for user input. At top of normal module.
Public Type InputUser
SeqNr As Integer
PlanCat As String * 15
Dept As String
End Type
'Declare Variables Arrays
Public aIU() As InputUser
Private Sub DefineUserInputs()
Dim Sht As Object 'Input sheet with data & Output sheet to range
Dim Rng As Range 'Find range with data
Dim Rq As Long 'Row count
Dim Cq As Long 'Column count
Dim x As Long ' loop counter
'Find sheet
Set Sht = Worksheets("WagesUserInputDB")
'Find range of input values
Set Rng = Sht.Range("A1").CurrentRegion
'Find quantity of rows and cols of range to resize array
Rq = Rng.Rows.Count
Cq = Rng.Columns.Count
ReDim aIU(Rq)
'Copy values from user input range TO user-defined ARRAY
For x = 1 To Rq
aIU(x).SeqNr = Rng.Cells(x, 1)
aIU(x).PlanCat = Rng.Cells(x, 2)
aIU(x).Dept = Rng.Cells(x, 3)
Next x
'Copy values FROM user-defined ARRAY to user input range
Set Rng = Worksheets("NewSht").Range("a1") 'Set variable to new sheet
Set Rng = Rng.Resize(UBound(aIU), 1) 'Resize Range = Array size
Rng = aIU ' Write array to sheet range
End Sub