0

我有以下代码:它工作正常,但我想在代码的最后部分改进它,如果 contador = 0,我想要 mespt="No se Considera" 而不是只返回 0。

    Public Function mespt(tutor As String, mes As String, j As Long) As Double


Application.Volatile

Dim a As Long
Dim totalmesp As Double


mespt = 0
contador = 0
totalmespt = 0
For i = 4 To 1000
If Sheets("Hoja1").Cells(i, 2).FormulaR1C1 = tutor And Sheets("Hoja1").Cells(i, 5).FormulaR1C1 = mes Then
Select Case Sheets("Hoja1").Cells(i, j).Value



Case "No cumple"
a = 0
contador = contador + 1
Case "Regular"
a = 1
contador = contador + 1
Case "Pleno"
a = 3
contador = contador + 1
Case "No se considera"
a = 0
End Select


totalmespt = totalmespt + a
If contador = 0 Then
mespt = 0
Else
mespt = totalmespt / contador
End If

End If
Next


End Function

我已经通过以下方式使用了变体:

Funcion mespt(                       ) as Variant
.......

if contador = 0 then
mespt="No se considera" then
mespt=totalmespt/contador
end if

end function

但在 contador=0 的情况下,该函数只返回 #!Valor

抱歉,使用变体类型,它可以正常工作,正如我现在所期望的那样,问题只是 Excel 中的一个公式与函数 mespt 一起使用。

4

2 回答 2

1

使用 JSJ 所说的变体。

VBA 自动将 Variant 类型转换为适当的类型。在下面的示例中,函数根据函数的参数将其返回值设置为布尔值或字符串值。

Private Function returnVariant(returnBoolean As Boolean) As Variant

    If returnBoolean Then
        returnVariant = False
    Else
        returnVariant = "Hi this is a string"
    End If

End Function

Private Sub showFunctionExample()
    Dim v As Variant
    Dim v2 As Variant

    v = returnVariant(True)
    v2 = returnVariant(False)

    Debug.Print CStr(v) + "- Type: " + CStr(TypeName(v))
    Debug.Print v2 + "- Type:" + TypeName(v2)
End Sub

对于您的代码,请执行以下操作:

Public Function mespt(tutor As String, mes As String, j As Long) As Variant


Application.Volatile

Dim a As Long
Dim totalmesp As Double


mespt = 0
contador = 0
totalmespt = 0
For i = 4 To 1000
If Sheets("Hoja1").Cells(i, 2).FormulaR1C1 = tutor And Sheets("Hoja1").Cells(i, 5).FormulaR1C1 = mes Then
Select Case Sheets("Hoja1").Cells(i, j).Value



Case "No cumple"
a = 0
contador = contador + 1
Case "Regular"
a = 1
contador = contador + 1
Case "Pleno"
a = 3
contador = contador + 1
Case "No se considera"
a = 0
End Select


totalmespt = totalmespt + a
If contador = 0 Then
mespt="No se considera" 
Else
mespt = totalmespt / contador
End If

End If
Next


End Function

请注意,您必须小心将此函数分配给本身不属于 Variant 类型的变量,因为如果您返回字符串并将其分配给双精度值,则会出现错误。

于 2013-10-10T17:28:04.543 回答
0

我不会使用 Variant 方法,因为稍后在尝试使用返回设置变量时可能会遇到错误。我会在所有帐户上作为字符串返回。

Public Function mespt(tutor As String, mes As String, j As Long) As String


Application.Volatile

Dim a As Long
Dim totalmesp As Double


mespt = 0
contador = 0
totalmespt = 0
For i = 4 To 1000
If Sheets("Hoja1").Cells(i, 2).FormulaR1C1 = tutor And Sheets("Hoja1").Cells(i, 5).FormulaR1C1 = mes Then
Select Case Sheets("Hoja1").Cells(i, j).Value



Case "No cumple"
a = 0
contador = contador + 1
Case "Regular"
a = 1
contador = contador + 1
Case "Pleno"
a = 3
contador = contador + 1
Case "No se considera"
a = 0
End Select


totalmespt = totalmespt + a
If contador = 0 Then
mespt = 0
Else
mespt = totalmespt / contador
End If

End If
Next


End Function

然后在使用它时测试返回

Sub Sample()
IF IsNumeric(mespt("a","b","c") Then
    'Code if it comes back with a number
Else
    'Code to run if it doesn't not return number
End If
End Sub

或者

=if(ISNUMBER(mespt(a,b,c)), "What to do If Number is Returned", "What To do if NON-Number is returned")

IsNumeric如果 Expression 的数据类型是BooleanByteDecimalDoubleIntegerLongSByteShortSingleUIntegerULongUShort,或者是包含其中一种数字类型的 Object ,则返回True。如果 Expression 是可以成功转换为数字的 Char 或 String,它也返回 True。

IsNumeric如果 Expression 是数据类型Date或数据类型Object 并且不包含数字类型,则返回False

IsNumeric如果 Expression 是不能转换为数字的Char或String ,也会返回False

于 2013-10-10T17:40:52.960 回答