Good day,
I'm trying to create a macro for a dispersion formula.
The formula is
D = [ sum from 1 to Nt( {(X - Xave)}^2 / Nt) ]^ 0.5
I need the answer to be in cell F2 and I'm trying to avoid having a new column containing the data,
{(X - Xave)}^2 / Nt)
although I make do with it.
Xs are in Column A
Nt is the number of Xs i.e the number of rows in column A with data. It is unknown but I know there is a code for finding the last occupied cell in a column so it can be found.
Xave is the average of the data.
D is the value I'm trying to calculate.
E.g Z = x + y where x and y are data in colunms A and B respectively. Z would now be a new colunm populated with x + y.
This code will be used for different workbooks, same sheet (1) but different values of Nt.
When I recorded a macro for one on of the sheet, this is what I got
ActiveCell.FormulaR1C1 = "=AVERAGE(R[-10201]C:R[-2]C)"
Range("E10204").Select
Range("F2").Select
ActiveCell.FormulaR1C1 = "=((RC[-1]-R10203C[-1])^2)/COUNT(R2C[-1]:R10201C[-1])"
Selection.AutoFill Destination:=Range("F2:F10201")
Range("F10203").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-10201]C:R[-2]C)"
Range("G10203").Select
ActiveCell.FormulaR1C1 = "=SQRT(RC[-1])"
But this is for one sheet with the the number of rows known and I was forced to create another column to carry out the calculation. I really need this macro as I have about 2.2 GB worth of data to sift through.
My preliminary attempt was this
Sub Macro2()
Dim LR As Long
Dim Mean
Dim Total
Dim i As Long
LR = .Range("A" & .Rows.Count).End(xlUp).Row
Mean = Average("A2:LR")
Total = 0
For i = 2 To LR
Subtotal = (Ai - Mean) ^ 2
Total = Total + Subtotal
Next i
ActiveCell.Formula = "=SQRT(Total)"
End Sub
Thanks
P.S I know that the underlined portion of the code obtains the last row but I don't understand how.
Bookmarks