2013-10-10 161 views
0

我有以下代码:它工作正常,但我想要改善它的代码的最后部分,如果contador = 0,我想要mespt =“没有本质considera”,而不仅仅是返回0。VBA Excel-函数返回一个数字或字符串

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 

I`ve下列方式使用变种:

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

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

end function 

但在康塔多的情况下= 0时,该功能只是返回#!Valor

对不起,机智h变种类型它的工作流程OK,正如我所期望的那样,问题只是在excel中使用函数mespt的公式。

+3

如何返回为Varient。 – JSJ

+0

@jsj:这是一个有效的答案。 :)你可能想把它作为答案? –

+0

请在我的帖子中查看我的代码的执行情况以及您的建议,但不幸运。 – CreamStat

回答

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 

请注意,你必须要小心分配该功能到是变的不是类型本身,你会如果得到错误变量您返回一个字符串并将其分配给一个double。

+0

是否有可能使它在我将用作自定义工作表函数的VBA函数中工作?我测试了你的returnVariant函数,它似乎不起作用。背景 - 我想返回一个double或一个错误消息作为字符串。 –

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 

OR

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

IsNumeric回报如果表达式的数据类型为布尔字节十进制整数为SByteUIntegerULONG,或USHORT,或包含其中一种数字类型的对象。如果Expression是可以成功转换为数字的Char或String,它也会返回True。

IsNumeric回报如果表达式的数据类型为日期或数据类型对象,它不包含数字类型

IsNumeric也返回如果表达式是一个字符字符串不能被转换为数字

相关问题