Visual Basic - Funcion DesvEstIf() (Desvío estandar condicional)

Life is soft - evento anual de software empresarial
 
Vista:
sin imagen de perfil
Val: 12
Ha aumentado su posición en 4 puestos en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Funcion DesvEstIf() (Desvío estandar condicional)

Publicado por Ismael (7 intervenciones) el 08/04/2017 04:27:06
Desvío estándar con criterio (numéricos y/o string) para rangos o matrices, omite valores/celdas nulas o vacías:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Public Function DstDevIf([Arg1] As Range, [Arg2] As Object, Optional [Arg3] As Range = Nothing) As Double
 
        Dim Avrge, mtras, varianze, suma As Double
        Dim NumColumn As Integer
 
        DstDevIf = Nothing
 
        If [Arg3] Is Nothing Then [Arg3] = [Arg1]
 
        Try
            NumColumn = [Arg3].Column - [Arg1].Column
            For Each R As Range In [Arg1]
                If CBool(WorksheetFunction.CountIf(R, [Arg2])) Then
                    For i = NumColumn To NumColumn + [Arg3].Columns.Count - 1
                        If Not String.IsNullOrEmpty(CStr(R.Offset(0, i).Value)) Then
                            suma = suma + CDbl(R.Offset(0, i).Value)
                            mtras = mtras + WorksheetFunction.CountA((CDbl(R.Offset(0, i).Value)))
                        End If
                    Next
                End If
            Next
            Avrge = suma / mtras
            For Each R As Range In [Arg1]
                If CBool(WorksheetFunction.CountIf(R, [Arg2])) Then
                    For i = NumColumn To NumColumn + [Arg3].Columns.Count - 1
                        If Not String.IsNullOrEmpty(CStr(R.Offset(0, i).Value)) Then
                            varianze = varianze + WorksheetFunction.Power((CDbl(R.Offset(0, i).Value) - Avrge), 2)
                        End If
                    Next
                End If
            Next
            DstDevIf = Math.Sqrt(varianze / (mtras - 1))
        Catch ex As Exception
            MessageBox.Show(CStr("Criterio no encontrado"))
        End Try
 
    End Function


Para usarlo:

1
DstDevIf([Arg1], [Arg2],[Arg3])


[Arg1] Argumento rango criterio Excel
[Arg2] Argumento criterio
[Arg3] Argumento rango a aplicar la desviación estándar. Si se omite, se usará el rango [Arg1]

Es rebuscado pero funciona bien... no olvidar declarar WorkSheetFunction, por ejemplo:

1
HojaExcel.Application.WorkSheetFunction
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder