Question: I tried to write this formula excel vba what is my wrong

```
For i = 1 To (N - 1)
    H = H + N- (i / (N) ^ P)
Next

```

[![enter image description here](https://i.stack.imgur.com/WYgWl.jpg)](https://i.stack.imgur.com/WYgWl.jpg)

Answer: 
The loop is only for what is behind the sum sign `Σ` and the `N - Σ` must be calculated **after** the loop. Also you need to place the perenthesis correctly:

```
Public Function H(ByVal N As Long, ByVal P As Double) As Double
    'Calculate the sum Σ
    Dim S As Double 'result of the sum Σ
    Dim i As Long
    For i = 1 To (N - 1)
        S = S + ((i / N) ^ P)
    Next

    'Then calculate the rest
    H = N - S
End Function

```
For caulations, VBA follows operator precedence, so we should carefully use parentheses around the calculations in order to change the predence of operators. Absence of parentheses at the right place in the above caluclations could be the reason of unexpected result. Please refer to the link below to see the operator precendence:

<https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/operator-precedence>