繁体   English   中英

VBA UDF返回数组

[英]VBA UDF Return Array

我有以下UDF,它需要遍历工作表上的所有数据,并返回学生的姓名和班级名称(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

如果要返回数组,则可以将函数定义为Variant,但最好将函数头更改为Variant(这样可以更轻松地直接查看函数的return-type):

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

在最后一行( TTDisplay = Result(1) )上,您仅返回一个值,因此将其更改为返回整个数组: TTDisplay = Result

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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