2013-10-09 89 views
0

我有我的代碼爲我的函數:我在j函數的輸出是一列的位置。在函數內部,「i」查找與變量「tutor」和「mes」相匹配的行的位置,然後給出單元格的值(i,j)。EXCEL VBA函數與SELECT CASE,ELSE

單元格(i.j)的值來自Excel表單的列表。列表的值是:「沒有cumple」,「Regular」,「Pleno」,「不考慮」。在內部,函數將0分爲「無累加」,1爲「正常」,3爲「Pleno」,0爲「不考慮」。

這些函數旨在計算每個輸入的值的均值(「導師」,「mes」,「j」)

當案件「沒有考慮」是在一個單元格中,這意味着如果,例如,有5個值必須calculare的意思是「不考慮」,所以函數只需要計算4個值的平均值。

問題是函數在單元格(ij)的值不起作用時,是「沒有考慮」

代碼如下:

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 
+0

你'康塔=康塔多 - 1'其次是'康塔=康塔+ 1' – user2140261

回答

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肯定這是你在找什麼。

+0

例如,什麼是預期的是,如果有之前的「無SE considera」和值我已經計算意思是4個值,那麼不考慮「不考慮」,並且均值是相同的,因爲值的總和相同並且所考慮的值的數量相同。問題在於當位置j給出「不考慮」時,函數不計算正確的均值。 – CreamStat

+0

我覺得有一個missunderstadning。爲了更加精確,我將給出以下示例:我們有mespt(「Jean」,「June」,6),並且有三個單元格符合條件並且具有值「Pleno」,「Cumple」, 「沒有考慮到」。根據該算法,函數的值應爲「Pleno」值爲3,「Cumple」值爲1,「No se Considera」值爲0,並計算「Pleno」和「Cumple」的均值,「不考慮」 「沒有考慮到計算它,所以平均值是(3 + 0)/ 2和mespt = 2。再一次,問題是當它的值是」沒有考慮時「的單元格。 – CreamStat

+0

對於avobe在上述情況下,mespt中的所需輸出(「Jean」,「June」,6)爲2.但在Excel中,它返回#Valor – CreamStat

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