使用SELECT CASE,ELSE的EXCEL VBA函数

时间:2013-10-09 17:47:07

标签: database excel validation vba excel-vba

我有我的函数代码:我在j中的函数的输出是列的位置。在函数内部,“i”查找与变量“tutor”和“mes”匹配的行的位置,然后它给出单元格(i,j)的值。

单元格(i.j)的值来自一张excel的列表。列表的值是:“No cumple”,“Regular”,“Pleno”,“No se Considera”。在内部,函数为0表示“No cumple”,1“表示”Regular“,3表示”Pleno“,0表示”No se Considera“。

函数打算计算函数的每个输入值(“tutor”,“mes”,“j”)的平均值。

当单元格中出现“No se Considera”的情况时,表示if,例如,有5个必须计算均值的值,“不考虑”不被考虑,所以函数只需要计算4个值的平均值。

问题是当cell(i.j)的值为“No se considerationra”时,该函数不起作用。

代码如下:

 Public Function mespt(tutor As String, mes As String, j As Long)
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
Case "Regular"
a = 1
Case "Pleno"
a = 3
Case Else
contador = contador - 1
a = 0
End Select


totalmespt = totalmespt + a
contador = contador + 1
mespt = totalmespt / contador
End If
Next


End Function

2 个答案:

答案 0 :(得分:0)

 Public Function mespt(tutor As String, mes As String, j As Long)
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"
contador = contador + 1
a = 0
Case "Regular"
contador = contador + 1
a = 1
Case "Pleno"
contador = contador + 1
a = 3
Case Else
contador = contador - 1
a = 0
End Select


totalmespt = totalmespt + a
mespt = totalmespt / contador
End If
Next


End Function

这只是一个猜测,但我 PRETTY 确定它是您正在寻找的。

答案 1 :(得分:0)

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

    Dim totalmespt As Double
    Dim contador As Long
    Dim i As Long

    With Sheets("Hoja1")
        For i = 4 To 1000
            If .Cells(i, "B").Value & .Cells(i, "E").Value = tutor & mes _
            And InStr(1, "|no cumple|regular|pleno|", "|" & .Cells(i, j).Value & "|", vbTextCompare) > 0 Then
                contador = contador + 1
                totalmespt = totalmespt + WorksheetFunction.Match(.Cells(i, j).Value, Array("no cumple", "regular", "", "pleno"), 0) - 1
            End If
        Next i
    End With

    If contador > 0 Then mespt = totalmespt / contador Else mespt = 0

End Function