简体   繁体   中英

Return a User-defined Array from a function VBA-Excel

Hi I've been trying the different solution that this website gave me, but i can't make my code run. I have a user-defined Type() and a Sub() and a function. Like this:

Type Base
    UT As String
    N_Inic As Integer
    N_Fin As Integer
    Largo As Integer
End Type

Sub Test()

   Dim A() As Base

   A = Base()

End Sub

Function Base() As Base
    Sheets("ARISTAS").Select
    ActiveSheet.Cells(2, 1).Select
    j = 2
    b = 0
    Set UT_Range = Range(ActiveCell, Cells(Rows.Count,_ Selection.Column).End(xlUp))
    Total_1 = UT_Range.Count
    Dim Base_UT() As Base
    ReDim Base_UT(Total_1)

    While Sheets("ARISTAS").Cells(j, 1).Value
        Base_UT(b).UT = Sheets("ARISTAS").Cells(j, 1).Value 
        Base_UT(b).N_Inic = Sheets("ARISTAS").Cells(j, 2).Value
        Base_UT(b).N_Fin = Sheets("ARISTAS").Cells(j, 3).Value
        Base_UT(b).Largo = Sheets("ARISTAS").Cells(j, 9).Value '**
        b = b + 1
        j = j + 1
    Wend
    Base = Base_UT
End Function

When I run my sub it said that can't assign to a matrix and highlight "A"

Does Anyone knows why?

Thanks you So Much

You're declaring A() as an array of type Base and you're also including a function called Base . From a maintenance and readibility perspective, this is a bad idea, I would strongly recommend giving a different name to your function.

That said, using simple example I can get this to work by tweaking the function signature to return an array:

Sub Test()

   Dim a() As Base

    a = GetBase()

End Sub

Function GetBase() As Base()
    Dim Total_1 As Long
    Dim b As Long
    b = 0
    Total_1 = 0
    Dim Base_UT() As Base
    ReDim Base_UT(Total_1)

        Base_UT(b).UT = "string"
        Base_UT(b).N_Inic = 1
        Base_UT(b).N_Fin = 2
        Base_UT(b).Largo = 3

    GetBase = Base_UT
End Function

instead of:

Function Base() As Base

use:

Function Base() As Base()

but there other possible faults in your code, that the following code should help to avoid

Function Base1() As Base()
    Dim b As Long, Total_1 As Long
    Dim UT_Range As Range, cell As Range

    With sheets("ARISTAS")
        Set UT_Range = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
        Total_1 = UT_Range.Count - 1
        ReDim Base_UT(Total_1) As Base

        For Each cell In UT_Range.SpecialCells(XlCellType.xlCellTypeConstants)
            Base_UT(b).UT = cell.Value
            Base_UT(b).N_Inic = cell.Offset(, 1).Value
            Base_UT(b).N_Fin = cell.Offset(, 2).Value
            Base_UT(b).Largo = cell.Offset(, 8).Value '**
            b = b + 1
        Next cell
        ReDim Preserve Base_UT(b - 1)
    End With

    Base1 = Base_UT
End Function

as you see I also changed the function name not to match the UDT name

Here is a very simple example based on your question:

Type Base
    UT As String
    N_Inic As Integer
    N_Fin As Integer
    Largo As Integer
End Type

Sub MAIN()
    Dim alpha As Base
    alpha = whatever()
    MsgBox alpha.UT & alpha.N_Inic & alpha.N_Fin & alpha.Largo
End Sub

Public Function whatever() As Base
    Dim poiuyt As Base

    poiuyt.UT = "hello"
    poiuyt.N_Inic = 11
    poiuyt.N_Fin = 17
    poiuyt.Largo = 19
    whatever = poiuyt
End Function

It uses an internal variable of Type Base within the function and just passes it back:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM