我需要一个功能,可以确定学生是在第一年,第二年还是第三年,或者他们是否已经毕业,基于a)他们正在学习的证书和b)他们的学习日期启动。
这是我的第一次努力 - 非常感谢任何反馈:
[编辑:问题是该函数在Excel中使用时返回错误]
[EDIT2:将DateDiff部分中的m更改为“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
答案 0 :(得分:0)
我建议更改为Select Case
,因为只返回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