Below we will look at a program in Excel VBA that calculates any term Tk and summation of terms up to N.
Explanation: the user has the option to enter "All" or "Odd", to respectively calculate the sum of the first N terms of the series or the sum of only the odd terms up to N.
Place a command button on your worksheet and add the following code lines:
1. First, we declare four variables of type Integer and one variable of type String.
Dim i, term, N, stepSize As Integer2. Second, we initialize the variables.
Dim sumType As String
i = 03. Empty the fields.
N = Range("C2").Value
sumType = Range("C3").Value
Range("A8:B1000").Value = ""4. Determine stepSize.
Range("C6").Value = ""
Select Case sumTypeTip: go through our Select Case program to learn more about the Select Case structure.
Case Is = "All"
stepSize = 1
Case Is = "Odd"
stepSize = 2
Case Else
MsgBox "Enter a valid expression in cell C3"
End
End Select
5. Do the calculations.
For term = 1 To N Step stepSizeExplanation: we use the Step keyword to specify the increment (1 for "All" and 2 for "Odd") for the counter variable of the loop.
Cells(8 + i, 1).Value = term
Cells(8 + i, 2).Value = (term ^ 2 + (6 * term) + 1) / ((9 * term) + 7)
Range("C6").Value = Range("C6").Value + Cells(8 + i, 2).Value
i = i + 1
Next term
Result:
.