0

I want to pick the number of rows dynamically for my standard deviation calculation in VBA.

Currently I have the following code:

Sub Stdev()

Dim x as integer
x=10
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "=STDEV(R[1]C:R[5]C)"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "=STDEV(R[1]C:R[x]C)"'this is my attempt at dynamic calculation
End Sub

How do I correct it to select the number of rows as specified in x?

1 Answer 1

1

Try:

Range("E1").FormulaR1C1 = "=STDEV(R[1]C:R[" & x & "]C)"
Sign up to request clarification or add additional context in comments.

3 Comments

if I want to make it a function instead of a sub, how do I do that?
what do you want as input and as output of the function?
As input, I want the flexibility to select random range of rows (say 1 to 30 or 4 to 19 etc., so I pass the beginning and ending row number as parameters to the function), and I want to get the standard deviation as output of that specified range of cells.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.