VBA UDF返回数组

时间:2016-07-19 06:30:43

标签: excel vba excel-vba udf

我有以下UDF需要遍历名为Classes的工作表上的所有数据,如果学生姓名显示在工作表的列表中,则返回学生姓名和班级名称(A列和B列)称为时间表(此列表位于单元格BM3到BM21中),类在UDF中输入的日期和时间进行。目前它返回#Value错误。我做错了什么?

Function TTDisplay(Day As String, Time As Variant) As Variant

Dim Result(1 To 12) As String
Dim Students As String
Dim cell As Integer
Dim LastRow As Long
Dim Classes As Worksheet
Dim Timetable As Worksheet
Dim x As Integer
Dim TimeSpan As Integer
Dim TTTime As Integer

Classes = Sheets("Classes")
Timetable = Sheets("Timetable")
LastRow = Classes.Cells(Classes.Rows.count, "A").End(xlUp).Row
TTTime = TMins(Time)

For cell = 3 To 21
Students = Students & Timetable.Cells(cell, 65).value & "!"
Next cell

x = 1
For cell = 2 To LastRow

        If InStr(Students, Classes.Cells(cell, 2)) Then
            If Day = Classes.Cells(cell, 9) Then
                If Time = Classes.Cells(cell, 12) Then
                Result(x) = Classes.Cells(cell, 2) & Chr(10) & Classes.Cells(cell, 1)
                x = x + 1
                Else
                TimeSpan = TMins(Classes.Cells(cell, 12)) + 30
                    Do While TimeSpan < TMins(Classes.Cells(cell, 11))
                        If TimeSpan = TTTime Then
                        Result(x) = Classes.Cells(cell, 2) & Chr(10) & Classes.Cells(cell, 1)
                        x = x + 1
                        GoTo MoveOn
                        Else
                        TimeSpan = TimeSpan + 30
                        End If
                    Loop
MoveOn:
                End If
            End If
        End If

Next cell
TTDisplay = Result(1)
End Function

2 个答案:

答案 0 :(得分:2)

如果要返回一个数组,可以将该函数定义为Variant,但最好将函数头改为this(这样可以更容易直接看到函数的返回类型):

Function TTDisplay(Day As String, Time As Variant) As String()

在最后一行(TTDisplay = Result(1))上,您只返回一个值,因此更改它以返回整个数组:TTDisplay = Result

答案 1 :(得分:0)

TTDisplay = Result(1)正在将UDF的值设置为数组中的第一个元素。 TTDisplay = Result将返回完整数组 您还必须使用 ctrl + shift + 输入 {=TTDisplay($A2,$B2)}将公式作为数组公式输入。

我修改了你的代码,让数组变得动态。

Function TTDisplay(Day As String, Time As Variant) As Variant

    Dim Result() As String
    Dim Students As String
    Dim cell As Integer
    Dim LastRow As Long
    Dim Classes As Worksheet
    Dim Timetable As Worksheet
    Dim x As Integer
    Dim TimeSpan As Integer
    Dim TTTime As Integer

    Classes = Sheets("Classes")
    Timetable = Sheets("Timetable")
    LastRow = Classes.Cells(Classes.Rows.Count, "A").End(xlUp).Row
    TTTime = TMins(Time)

    For cell = 3 To 21
        Students = Students & Timetable.Cells(cell, 65).Value & "!"
    Next cell

    x = 0
    ReDim Result(x)

    For cell = 2 To LastRow

        If InStr(Students, Classes.Cells(cell, 2)) Then
            If Day = Classes.Cells(cell, 9) Then
                If Time = Classes.Cells(cell, 12) Then
                    ReDim Preserve Result(x)
                    Result(x) = Classes.Cells(cell, 2) & Chr(10) & Classes.Cells(cell, 1)
                    x = x + 1
                Else
                    TimeSpan = TMins(Classes.Cells(cell, 12)) + 30
                    Do While TimeSpan < TMins(Classes.Cells(cell, 11))
                        If TimeSpan = TTTime Then
                            ReDim Preserve Result(x)
                            Result(x) = Classes.Cells(cell, 2) & Chr(10) & Classes.Cells(cell, 1)
                            x = x + 1
                            GoTo MoveOn
                        Else
                            TimeSpan = TimeSpan + 30
                        End If
                    Loop
MoveOn:
                End If
            End If
        End If

    Next cell
    TTDisplay = Result
End Function
相关问题