How to Calculate Fibonacci in VBA Word

Save

The Microsoft Office Word text processing program includes the Visual Basic for Applications scripting tool that allows users to insert calculated fields, such as a Fibonacci sequence of numbers, in their text documents. The Fibonacci numbers are the integer sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, ..., in which each item is the sum of the previous two numbers in the sequence. By implementing a recursive loop in your VBA script, you can calculate a Fibonacci sequence in VBA Word.

  • Click the "Start" button in Windows and select the "Microsoft Word" program.

  • Click "Tools" and then "Scripts" to create the VBA script for calculating a Fibonacci sequence.

  • Enter the following code in the window:

    Function fibonacci(ByVal n as Integer)

    If (n <= 0) Then

    fibonacci = 0

    ElseIf (n = 1) Then

    fibonacci = 1

    Else

    fibonacci = fibonacci(n-1) + fibonacci(n-2)

    End If

    End Function

  • Click "File" and then "Save" to save the "fib.doc" Word document with the VBA script for calculating Fibonacci sequence of numbers.

References

  • Photo Credit Jupiterimages/Photos.com/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!