VBA gemiddelde kolom

Status
Niet open voor verdere reacties.

Cindy B.

Gebruiker
Lid geworden
6 mei 2002
Berichten
321
Ik heb het onderstaande stukje VBA in een database zitten. Deze telt de getallen van 1 kolom op, en zet in de kolom erna het gemiddelde ervan.
Om te kunnen sorteren zouden 1-cijferige getallen weergegeven moeten worden als 02, 03,04 e.d.
Dit gebeurt nu niet, er staat nu gewoon 2, 3 en 4.

Wie weet hoe ik dit aan kan passen?

Groetjes,
Cindy

Function potplant(a)
g1 = Val(Mid$(a & "-00-00-00-00-00-00", 1, 2))
g2 = Val(Mid$(a & "-00-00-00-00-00-00", 4, 2))
g3 = Val(Mid$(a & "-00-00-00-00-00-00", 7, 2))
g4 = Val(Mid$(a & "-00-00-00-00-00-00", 10, 2))
g5 = Val(Mid$(a & "-00-00-00-00-00-00", 13, 2))
g6 = Val(Mid$(a & "-00-00-00-00-00-00", 16, 2))
If Len(a) < 2 Then
potplant = 0
Else
If Len(a) < 3 Then
potplant = g1
Else
If Len(a) < 6 Then
potplant = Int((g1 + g2) / 2)
Else
If Len(a) < 9 Then
potplant = Int((g1 + g2 + g3) / 3)
Else
If Len(a) < 12 Then
potplant = Int((g1 + g2 + g3 + g4) / 4)
Else
If Len(a) < 15 Then
potplant = Int((g1 + g2 + g3 + g4 + g5) / 5)
Else
potplant = Int((g1 + g2 + g3 + g4 + g5 + g6) / 6)
End If
End If
End If
End If
End If
End If

End Function
 
Public Function potplant(a) As String

g1 = Val(Mid$(a & "-00-00-00-00-00-00", 1, 2))
g2 = Val(Mid$(a & "-00-00-00-00-00-00", 4, 2))
g3 = Val(Mid$(a & "-00-00-00-00-00-00", 7, 2))
g4 = Val(Mid$(a & "-00-00-00-00-00-00", 10, 2))
g5 = Val(Mid$(a & "-00-00-00-00-00-00", 13, 2))
g6 = Val(Mid$(a & "-00-00-00-00-00-00", 16, 2))

Select Case Len(a)
Case Is < 2
potplant = 0
Case Is < 3
potplant = CStr(g1)
Case Is < 6
potplant = CStr(Int((g1 + g2) / 2))
Case Is < 9
potplant = CStr(Int((g1 + g2 + g3) / 3))
Case Is < 12
potplant = CStr(Int((g1 + g2 + g3 + g4) / 4))
Case Is < 15
potplant = CStr(Int((g1 + g2 + g3 + g4 + g5) / 5))
Case Else
potplant = CStr(Int((g1 + g2 + g3 + g4 + g5 + g6) / 6))
End Select

If Len(potplant) = 1 Then
potplant = "0" & potplant
End If

End Function
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan