VBA Excel-返回数字或字符串的函数

时间:2013-10-10 17:14:09

标签: algorithm excel vba analytics

我有以下代码:它工作正常,但我想改进它在代码的最后部分,如果contador = 0,我想要mespt =“没有考虑到”而不是只返回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一起使用。

2 个答案:

答案 0 :(得分:1)

使用Variant作为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类型的变量,因为如果返回字符串并将其指定为double,则会出现错误。

答案 1 :(得分: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")
如果Expression的数据类型为布尔字节十进制,则

IsNumeric会返回 True 双重整数 SByte UInteger ULong UShort ,或包含其中一种数字类型的对象 。如果Expression是可以成功转换为数字的Char或String,它也会返回True。

如果Expression的数据类型为日期,或者数据类型为对象,则

IsNumeric会返回 False ,并且它不包含数字类型

如果Expression是 Char 无法转换为数字的字符串,

IsNumeric也会返回 False