I am new to VBA Marco for Excel. I am still learning it. I had only basic knowledge of it. I am doing a project, I need some helps with the code. What I need to do is to count Standard Deviation and Mean. I got the code from Anthony’s vba page.. But I need further improve to do something different. Sorry, I am still quite weak in macro. Thus, I need help with it.
Below is the code to calculate std.dev and mean.
This code work well. The problem is that it counts from cells A1 to A10. So I must have values enter in the 10 cells in order to get the correct values. This mean if one of the cells is blank, its will count the blank cell with a value of 0. The outcome result of Mean on cell A12 and Standard Deviation on cell A13 will be wrong.
What I want to do ??
In the excel sheet which I am working on. There are around 10 different names of items.
Example, cell A1 have the item name as “AB” and its “value” is in cell B1. Cell A2 have the item name as “CD” and its “value” is in cell B2.
There are about 10 different names with values in the cells beside them, over 60 thousand data and all are in disorderly manner. What I want is like improve on this code so it can count the right number of cells for each item instead of up to 10 rows. It is like getting the last row of each item. So I can get Mean and Standard Deviation of each of the Item correctly.
I still trying to figure out how to code it, really need help.
Thank a lot!
Below is the code to calculate std.dev and mean.
Code:
Sub compute()
Dim Arr(10) As Single
Dim Average As Single
Dim Std_Dev As Single
For i = 1 To 10
Arr(i) = Sheets("Sheet1").Cells(i, 1)
Next i
Average = Mean(10, Arr)
Std_Dev = StdDev(10, Arr)
Sheets("Sheet1").Cells(12, 1) = Average
Sheets("Sheet1").Cells(13, 1) = Std_Dev
End Sub
Function Mean(k As Long, Arr() As Single)
Dim Sum As Single
Dim i As Integer
Sum = 0
For i = 1 To k
Sum = Sum + Arr(i)
Next i
Mean = Sum / k
End Function
Function StdDev(k As Long, Arr() As Single)
Dim i As Integer
Dim avg As Single, SumSq As Single
avg = Mean(k, Arr)
For i = 1 To k
SumSq = SumSq + (Arr(i) - avg) ^ 2
Next i
StdDev = Sqr(SumSq / (k - 1))
End Function
This code work well. The problem is that it counts from cells A1 to A10. So I must have values enter in the 10 cells in order to get the correct values. This mean if one of the cells is blank, its will count the blank cell with a value of 0. The outcome result of Mean on cell A12 and Standard Deviation on cell A13 will be wrong.
What I want to do ??
In the excel sheet which I am working on. There are around 10 different names of items.
Example, cell A1 have the item name as “AB” and its “value” is in cell B1. Cell A2 have the item name as “CD” and its “value” is in cell B2.
There are about 10 different names with values in the cells beside them, over 60 thousand data and all are in disorderly manner. What I want is like improve on this code so it can count the right number of cells for each item instead of up to 10 rows. It is like getting the last row of each item. So I can get Mean and Standard Deviation of each of the Item correctly.
I still trying to figure out how to code it, really need help.
Thank a lot!