Option Explicit
Function resid(ByVal target As String, ByVal cwl As String) As Single
Dim target_calc As Single, cwl_calc As Single
target_calc = convert_grade(target)
cwl_calc = convert_grade(cwl)
Dim tmp_cwl As Single: tmp_cwl = Round(cwl_calc - Int(cwl_calc), 1)
Dim tmp_target As Single: tmp_target = Round(target_calc - Int(target_calc), 1)
Select Case Int(cwl_calc)
Case Is > Int(target_calc) ' Over Target
If (Int(cwl_calc) - Int(target_calc) > 1) Then
If tmp_cwl < tmp_target Then
resid = Int(cwl_calc) - Int(target_calc)
resid = resid + IIf(tmp_target = 0.3, tmp_cwl, tmp_target) - 1
Else
resid = cwl_calc - target_calc
End If
Else
If tmp_cwl < tmp_target Then
resid = IIf(tmp_target = 0.3, tmp_cwl, tmp_target)
Else
resid = cwl_calc - target_calc
End If
End If
Case Is < Int(target_calc) ' Under Target
If (Int(cwl_calc) - Int(target_calc) < -1) Then
If tmp_cwl > tmp_target Then
resid = Int(cwl_calc) - Int(target_calc)
resid = resid - IIf(tmp_cwl = 0.2, tmp_cwl, tmp_target) + 1
Else
resid = cwl_calc - target_calc
End If
Else
If tmp_cwl > tmp_target Then
resid = IIf(tmp_cwl = 0.2, tmp_cwl, tmp_target)
resid = -resid
Else
resid = cwl_calc - target_calc
End If
End If
Case Else
resid = cwl_calc - target_calc
End Select
End Function
Private Function convert_grade(ByVal grade As String) As Single
Dim grade_split(0 To 1) As Single
grade_split(0) = Mid(grade, 1, 1)
Select Case UCase(Mid(grade, 2, 1))
Case "C"
grade_split(1) = 0.1
Case "B"
grade_split(1) = 0.2
Case "A"
grade_split(1) = 0.3
Case Else
grade_split(1) = 9000
End Select
convert_grade = grade_split(0) + grade_split(1)
End Function