2016-07-19 10 views
0

私はClassesという名前のシート上のすべてのデータをループし、生徒の名前がクラスに表示される場合はクラス名(列AとB)を返す必要がある次のUDFを持っています。タイムテーブルと呼ばれるシート上のリスト(このリストはセルBM3からBM21にあります)、クラスはUDFに入力された日時に実行されます。現在#Valueエラーを返します。私は何を間違えたのですか?VBA UDF戻り配列

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 
+0

「結果」は1から12ですが、12個以下のエントリはありますか? 'For cell = 2 To LastRow'の直後に' If x = 13 Then Exit For'を入れて、もう一度チェックしてみてください。 –

答えて

2

あなたは、配列を返すようにしたい場合は、バリアントとしての機能を定義するが、好ましくは、(それが簡単に直接、関数の戻り値型を参照することができます)これにあなたの関数の頭を変更することができます。

Function TTDisplay(Day As String, Time As Variant) As String() 
あなたは一つの値だけを返している最後の行( TTDisplay = Result(1))で

、その全体の配列を返すように変更:TTDisplay = Result

0

TTDisplay = Result(1)を配列の最初の要素にあなたのUDFの値を設定しています。 TTDisplay = Resultは完全配列を返します。
また + {=TTDisplay($A2,$B2)}を入力CTRL +シフトを使用して、配列数式として数式あなたを入力する必要があります。

コードを変更して、配列を動的にしました。

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