如何将Excel工作表中的数组加载到VBA中?

wqnecbli  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(120)

我想写一个函数,将考试分数转换为分数。如果我在函数中定义了数组,它将按预期工作。但是我想给用户提供一个选项,将他自己的分数/分数结构用作函数的参数。
我试着从电子表格中加载一个数组
这一个给我一个Excel中的值错误。

Function GRADE(Points, ParamArray Pattern())

 If IsMissing(Pattern) Then Pattern = Array(90.5, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35)

 Grades = Array(1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 5)
 
 For i = LBound(Pattern) To UBound(Pattern)
  If Points >= Pattern(i) Then
   GRADE = Grades(i)
   Exit For
  End If
 Next i

End Function
x4shl7ld

x4shl7ld1#

评分体系自定义项

Option Explicit

Function GRADE(ByVal Points As Double, ParamArray Pattern()) As Double

    If IsMissing(Pattern) _
        Then Pattern = Array(90.5, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35)
    Dim Grades(): Grades = Array(1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 5)
 
    Dim Result As Double, i As Long
 
    For i = LBound(Pattern) To UBound(Pattern)
        If Points >= Pattern(i) Then Result = Grades(i): Exit For
    Next i
    
    If Result = 0 Then Result = 6
    
    GRADE = Result

End Function

相关问题