I am working on a project in which I have to generate a table of output from a range of data. I am able to generate Unique values from the list of data and I am able to calculate Count, Mean and Standard Deviation for the individual values. But, I am not able to connect these two functions. Can anyone tell a solution to how I can execute everything from one function?
I want to call the Uniques function in the code from the array output and then execute a mathematical function on these values.
Code
Option Explicit
Public Function uniques(myrange As Range)
Dim list As New Collection
Dim Ulist() As String
Dim value As Variant
Dim i As Integer
'Adding each value of myrang into the collection.
On Error Resume Next
For Each value In myrange
'here value and key are the same. The collection does not allow duplicate keys hence only unique values will remain.
list.Add CStr(value), CStr(value)
Next
On Error GoTo 0
'Defining the length of the array to the number of unique values. Since the array starts from 0, we subtract 1.
ReDim Ulist(list.Count - 1, 0)
'Adding unique value to the array.
For i = 0 To list.Count - 1
Ulist(i, 0) = list(i + 1)
Next
'Printing the array
uniques = Ulist
End Function
Public Function findtext(tofind As String, myrange As Range) As Integer
' Removed RA from dim
Dim i As Integer
Dim rcount As Integer
rcount = 0
For i = 1 To myrange.Rows.Count
'tofind = uniques(myrange.Cells.value)
If myrange(i, 1) = tofind Then
rcount = rcount + 1
End If
Next i
findtext = rcount
End Function
Public Function findavg(tofind As String, myrange As Range)
Dim avg As Double, rcount As Integer
Dim SUM As Double, findtext As Double
Dim i As Integer
SUM = 0
rcount = 0
For i = 1 To myrange.Rows.Count
If myrange(i, 1) = tofind Then
SUM = SUM + myrange(i, 2)
rcount = rcount + 1
avg = SUM / rcount
End If
Next i
findavg = avg
End Function
Public Function findstd(tofind As String, myrange As Range)
Dim std As Double, rcount As Integer
Dim SUM As Double, avg As Double, totalstd As Double
Dim i As Integer
SUM = 0
std = 0
rcount = 0
For i = 1 To myrange.Rows.Count 'i to active selection
If myrange(i, 1) = tofind Then 'criteria = "A"....etc
SUM = SUM + myrange(i, 2) 'sum in loop
rcount = rcount + 1 'add & count in loop
avg = SUM / rcount
End If
Next i
For i = 1 To myrange.Rows.Count
If myrange(i, 1) = tofind Then
std = std + (myrange(i, 2) - avg) ^ 2
End If
Next i
findstd = Sqr(std / (rcount - 1))
End Function
Function arrayutput(tofind As String, myrange As Range) As Variant
'we take it as zero because we haven't taken option base1
Dim Output(0, 2) As Variant
Output(0, 0) = findtext(tofind, myrange) 'first column
Output(0, 1) = findavg(tofind, myrange) 'second column
Output(0, 2) = findstd(tofind, myrange)
arrayutput = Output
End Function
Subfrom a function. But it cannot return anything. I mean it will only run...