function - Populating an array with values that fall below the averange and calculating the variance -
i trying select values one-dimensional range of data fall below average.
would code right? think there problem how place them array belowavg...
function moybelow(data range) variant dim integer dim n long redim belowavg() variant dim varian double dim somcar() variant n=worksheetfunction.count(data) rendmoy=worksheetfunction.average(data) i=1 n if data.cells(i).value < rendmoy belowavg(i).value = data(i).value end if nb = belowavg.count j=1 nb sumsq= sumsq + (belowavg(i) - rendmoy)^2 next j next varian = sumsq/nb end function
here's function returns array of doubles contains below average cells in range:
function getbelowaveragevalues(data range,rangeaverage double) double() dim filteredrangevalues variant dim belowaveragevaluescount long dim belowaveragevalues() double dim long if data.cells.count = 1 msgbox "only 1 cell" exit sub end if data.autofilter 1, "<" & rangeaverage filteredrangevalues = data.specialcells(xlcelltypevisible) belowaveragevaluescount = ubound(filteredrangevalues) redim belowaveragevalues(1 belowaveragevaluescount) = 1 belowaveragevaluescount belowaveragevalues(i) = filteredrangevalues(i, 1) next getbelowaveragevalues = belowaveragevalues data.autofilter end function
it uses autofilter
function , specialcells property filter , select relevant cells. uses call this:
sub test() dim datarange range dim rangeaverage double dim belowaveragevalues() double dim long dim varian double set datarange = selection rangeaverage = application.worksheetfunction.average(datarange ) belowaveragevalues = getbelowaveragevalues(datarange ,rangeaverage) i=1 ubound(belowaveragevalues) sumsq= sumsq + (belowaveragevalues(i) - rendmoy)^2 next varian = sumsq/nb end sub
this tested, may have error somewhere. main function tested , worked me.
Comments
Post a Comment