excelvba中的循环引用
我正在尝试编写一个简单的Excel VBA函数,从给定单元格中获取一个数字并指定一个等级,然后在当前活动单元格中写入该值excelvba中的循环引用,vba,excel,Vba,Excel,我正在尝试编写一个简单的Excel VBA函数,从给定单元格中获取一个数字并指定一个等级,然后在当前活动单元格中写入该值 Public Function Grade(rng As Range) Dim number As Double number = rng.Value If number >= 75 Then ActiveCell.Value = "A" ElseIf number >= 60 Then Activ
Public Function Grade(rng As Range)
Dim number As Double
number = rng.Value
If number >= 75 Then
ActiveCell.Value = "A"
ElseIf number >= 60 Then
ActiveCell.Value = "B"
ElseIf number >= 50 Then
ActiveCell.Value = "C"
ElseIf number >= 45 Then
ActiveCell.Value = "D"
ElseIf number < 45 Then
ActiveCell.Value = "F"
End If
End Function
公共功能等级(rng As范围)
双精度数字
数字=参考值
如果数字>=75,则
ActiveCell.Value=“A”
如果号码>=60,则
ActiveCell.Value=“B”
如果号码>=50,则
ActiveCell.Value=“C”
如果号码>=45,则
ActiveCell.Value=“D”
ElseIf数<45则
ActiveCell.Value=“F”
如果结束
端函数
在Excel中调用“=等级(A2)”时,我收到错误消息
有一个或多个循环引用,其中公式直接或间接引用自己的单元格
我缺少什么?您不使用函数执行操作。你不应该试图修改单元格或任何东西。(在代码中,每次重新计算函数时,它都会修改您选择的任何单元格。) 您只需要将函数的值设置为一个变量。当代码完成时,它将函数的值返回给调用它的任何对象。该函数可在Excel公式和VBA中使用
Public Function Grade(rng As Range)
Dim number As Double
number = rng.Value
If number >= 75 Then
Grade = "A"
ElseIf number >= 60 Then
Grade = "B"
ElseIf number >= 50 Then
Grade = "C"
ElseIf number >= 45 Then
Grade = "D"
ElseIf number < 45 Then
Grade = "F"
End If
End Function
公共功能等级(rng As范围)
双精度数字
数字=参考值
如果数字>=75,则
等级=“A”
如果号码>=60,则
等级=“B”
如果号码>=50,则
Grade=“C”
如果号码>=45,则
Grade=“D”
ElseIf数<45则
Grade=“F”
如果结束
端函数
您不使用函数执行操作。你不应该试图修改单元格或任何东西。(在代码中,每次重新计算函数时,它都会修改您选择的任何单元格。)
您只需要将函数的值设置为一个变量。当代码完成时,它将函数的值返回给调用它的任何对象。该函数可在Excel公式和VBA中使用
Public Function Grade(rng As Range)
Dim number As Double
number = rng.Value
If number >= 75 Then
Grade = "A"
ElseIf number >= 60 Then
Grade = "B"
ElseIf number >= 50 Then
Grade = "C"
ElseIf number >= 45 Then
Grade = "D"
ElseIf number < 45 Then
Grade = "F"
End If
End Function
公共功能等级(rng As范围)
双精度数字
数字=参考值
如果数字>=75,则
等级=“A”
如果号码>=60,则
等级=“B”
如果号码>=50,则
Grade=“C”
如果号码>=45,则
Grade=“D”
ElseIf数<45则
Grade=“F”
如果结束
端函数
Excel公式备选方案以防万一:
=LookUp(A1,{0,45,50,60,75;"F","D","C","B","A"})
Excel公式备选方案以防万一:
=LookUp(A1,{0,45,50,60,75;"F","D","C","B","A"})
将
activecell.value=
更改为grade=
将activecell.value=
更改为grade=