Excel 确定学生';s的学习年限,或者他们是否已经毕业
我需要一个函数,可以确定一个学生是否在第一年、第二年或第三年学习,或者他们是否已经毕业,基于a)他们学习的证书和b)他们开始学习的日期 这是我的第一次努力-任何反馈都将不胜感激: [编辑:问题是函数在Excel中使用时返回错误] [EDIT2:将DateDiff部分中的m改为“m”,消除了错误-现在的问题是,二年级和三年级的学生被标记为“过去的”]Excel 确定学生';s的学习年限,或者他们是否已经毕业,excel,function,vba,Excel,Function,Vba,我需要一个函数,可以确定一个学生是否在第一年、第二年或第三年学习,或者他们是否已经毕业,基于a)他们学习的证书和b)他们开始学习的日期 这是我的第一次努力-任何反馈都将不胜感激: [编辑:问题是函数在Excel中使用时返回错误] [EDIT2:将DateDiff部分中的m改为“m”,消除了错误-现在的问题是,二年级和三年级的学生被标记为“过去的”] Function YearCalc(start, course) 'Introduce length - the course lengt
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
功能年检(开始,课程)
'引入长度-以年为单位的课程长度
将长度设置为整数
'定义与特定课程相对应的长度
如果course=“Msc”,则
长度=3
ElseIf course=“Adv Dip”然后
长度=2
ElseIf course=“PG Cert”然后
长度=1
如果结束
作为整数的Dim lengthm
长度=(长度*12)
'将差异定义为两个日期之间的月差;
“今天的日期和课程开始的日期。
diff=DateDiff(“m”,开始,日期,星期一)
'将日期差异与课程长度进行比较,
'这样,如果差异大于特定课程的长度
'该学生被标记为'过去':
如果差异>=(长度),则
YearCalc=“过去”
'如果差异小于课程长度,则确定
“它们分为以下几年:
其他的
如果0我建议更改为选择Case
,因为只有返回1值的部分原因是双重比较。
0那么您遇到了什么问题呢?它没有返回值,调试它似乎没有任何帮助-实际上会在原始帖子中包含问题…返回了什么错误?DateDiff(“m”,start,…
不是DateDiff(m,start,
#value!-没有太大帮助。
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