Finance

Charts

Statistics

Macros

Search

Calculate the Fibonacci sequence in Excel

This code creates a VBA function that generates the Fibonacci sequence up to a specified term.

Steps to Implement the Fibonacci Sequence in VBA:

  1. Open the VBA Editor:
    • In Excel, press Alt + F11 to open the VBA editor.
    • In the editor, click Insert and then choose Module. This will add a new module where you can write your code.
  2. Write the VBA Code to Calculate the Fibonacci Sequence:

Here is the complete VBA code with detailed explanations.

Sub CalculateFibonacci()
    ' Declare variables
    Dim n As Integer
    Dim fib1 As Long, fib2 As Long, fib3 As Long
    Dim i As Integer   
    ' Ask the user for the number of Fibonacci terms to display
    n = InputBox("How many Fibonacci terms do you want to display?", "Input", 10)   
    ' Check if the input is valid
    If n <= 0 Then
        MsgBox "Please enter a number greater than 0.", vbExclamation
        Exit Sub
    End If   
    ' Initialize the first two terms of the Fibonacci sequence
    fib1 = 0
    fib2 = 1   
    ' Display the first two terms
    Range("A1").Value = fib1
    Range("A2").Value = fib2   
    ' Calculate the next terms and display them in column A
    For i = 3 To n
        ' The next term is the sum of the two previous terms
        fib3 = fib1 + fib2       
        ' Display the term in the corresponding cell in column A
        Cells(i, 1).Value = fib3       
        ' Update the values of the last two terms
        fib1 = fib2
        fib2 = fib3
    Next i   
    MsgBox "Calculation completed for " & n & " Fibonacci terms.", vbInformation
End Sub

Explanation of the Code:

  1. Variable Declarations:
    • n: This will be the number of terms the user wants to display from the Fibonacci sequence.
    • fib1 and fib2: The first two terms of the Fibonacci sequence.
    • fib3: The next term, which is calculated in each iteration.
    • i: A counter used in the For loop to iterate through the terms.
  2. User Input:
    • InputBox: A dialog box is displayed asking the user how many terms they want to see from the Fibonacci sequence. The input is stored in the variable n.
  3. Input Validation:
    • If the user enters a number less than or equal to 0, an alert appears, and the program exits.
  4. Initializing the First Two Terms:
    • The first two terms of the Fibonacci sequence are defined:
      • fib1 = 0 (the first term)
      • fib2 = 1 (the second term)
  5. Displaying Terms in Excel:
    • The first two terms (0 and 1) are directly displayed in cells A1 and A2 in Excel.
    • A For loop is used to calculate and display the subsequent terms until the user-specified number (n) is reached. In each iteration:
      • The next term is calculated as the sum of the previous two terms.
      • The term is displayed in the corresponding cell in column A (e.g., A3, A4, etc.).
  6. Updating Previous Terms:
    • After each iteration, the variables fib1 and fib2 are updated to hold the last two terms of the sequence.
  7. Final Message:
    • A message box appears indicating that the calculation is complete for the specified number of terms.

How to Use the Code:

  1. After pasting the code into the VBA editor, you can run it by pressing F5 in the editor or by assigning the macro to a button in your Excel sheet.
  2. The Fibonacci sequence will be displayed starting from cell A1 down to the cell corresponding to the number of terms requested.

Example:

If you enter « 10 » in the dialog box, the first 10 terms of the Fibonacci sequence will be displayed in cells from A1 to A10:

A1: 0
A2: 1
A3: 1
A4: 2
A5: 3
A6: 5
A7: 8
A8: 13
A9: 21
A10: 34
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx