How to Calculate Fibonacci in VBA Word

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Calculate Fibonacci in VBA Word
Word documents can contain VBA scripts for calculating numerical fields. (Jupiterimages/Photos.com/Getty Images)

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.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

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

  2. 2

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

  3. 3

    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

  4. 4

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

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.