Basically, I need to write a VBA function which evaluates the function "1/i" for values from i = 1 to n. n can be any integer larger than one. It then adds all those terms to get the final answer. For example, if n = 4, the sum would be (1/1) + (1/2) + (1/3) + (1/4) = 2.083
I can make the formula which evaluates 1/n, but not sure how to add the series starting at 1. I am referencing n from a value on the spreadsheet, and then pasting the final answer back onto the sheet.
So far here is what I have:
Option Explicit
Function series(n As Integer) As Double
series = (1 / n)
End Function
Sub sum()
Dim n As Integer, x As Double
With ThisWorkbook.Worksheets("Sheet1")
n = .Cells(1, 1)
End With
x = series(n)
With ThisWorkbook.Worksheets("Sheet1")
.Cells(2, 1).Value = x
End With
End Sub
Bookmarks