2012-06-25 27 views
0

我需要一個函數來確定學生是在他們的第一年,第二年還是第三年學習,或者他們是否已經畢業,基於a)他們的證書正在研究和b)他們開始的日期。確定學生的學習年份,或者他們是否已畢業

這是我的第一次努力 - 任何反饋,將不勝感激:

[編輯:問題是,該功能在Excel中使用它時,會返回一個錯誤]

[EDIT2:改變米的DateDiff的部分爲「M」,其淘汰的錯誤 - 現在只是2樓和3年級學生中被標記爲「過去」]問題

Function YearCalc(start, course) 

    'Introduce length - the course length in years 
    Dim length As Integer 

    'Define length corresponding to specific courses 

    If course = "Msc" Then 
     length = 3 
    ElseIf course = "Adv Dip" Then 
     length = 2 
    ElseIf course = "PG Cert" Then 
     length = 1 
    End If 

    Dim lengthm As Integer 

    lengthm = (length * 12) 

    'Define diff as the month difference between two dates; 
    'today's date and the date the course was started. 

    diff = DateDiff("m", start, Date, vbMonday) 

    'Compare the date difference to the length of the course, 
    'such that if the difference is larger than length of the specific course 
    'the student is marked as 'Past': 

    If diff >= (lengthm) Then 
     YearCalc = "Past" 

    'If the difference is less than the length of the course, determine which 
    'year they fall into: 

    Else 
     If 0 <= (diff - lengthm) < 1 Then 
      YearCalc = 1 
     ElseIf 1 <= (diff - lengthm) < 2 Then 
      YearCalc = 2 
     ElseIf 2 <= (diff - lengthm) < 3 Then 
      YearCalc = 3 
     End If 
    End If 

End Function 
+1

那你有什麼問題? –

+0

它沒有返回一個值,調試它似乎沒有發現任何有用的東西 - 實際上將問題包含在原始帖子中... – Jarose

+1

返回什麼錯誤? –

回答

0

我會建議更改爲Select Case,由於部分原因只有retu rning 1值是雙重比較。
0 <= AnyComparison將始終評估爲True

Select Case course 
    Case "Msc" 
     length = 36 
    Case "Adv Dip" 
     length = 24 
    Case "PG Cert" 
     length = 12 
End Select 

Select Case (diff - lengthm) 
    Case <0 
     YearCalc = "Past" 
    Case 0 to 11 
     YearCalc = 1 
    Case 12 to 23 
     YearCalc = 2 
    Case 24 to 35 
     YearCalc = 3 
    Case Else 
     YearCalc = "Course too long?" 
End Select