# Fibonacci Calculator

#### sliollio

##### New Member
Trying to get this to generate the Fibonacci sequence from 0 to that whatever number the user inputs and siplay the result in a messagebox. Very lost.

Sub Fib()
Dim Fibonacci As Variant
Dim n As Integer
n = InputBox("Enter a number")
If (n <= 0) Then
Fibonacci = 0
ElseIf (n = 1) Then
Fibonacci = 1
Else
Fibonacci = Fibonacci(n - 1) + Fibonacci(n - 2)
End If
MsgBox (Fibonacci)
End Sub

### Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

#### pgc01

##### MrExcel MVP
Hi

Since you are using a recursive algorithm you should separate the initialisation of the variables and the recursive algorithm.

I used your code and split it in the 2 parts:

Code:
``````Sub Fib()
Dim Fibonacci As Variant
Dim n As Long

n = InputBox("Enter a number")
Fibonacci = CalculateFib(n)
MsgBox (Fibonacci)
End Sub

Function CalculateFib(ByVal n As Long) As Long

If (n <= 0) Then
CalculateFib = 0
ElseIf (n = 1) Then
CalculateFib = 1
Else
CalculateFib = CalculateFib(n - 1) + CalculateFib(n - 2)
End If
End Function``````

#### Rick Rothstein

##### MrExcel MVP
Trying to get this to generate the Fibonacci sequence from 0 to that whatever number the user inputs and siplay the result in a messagebox. Very lost.
This question was asked yesterday by another individual. Here is the link to the infinite precision Fibonacci list calculator that I posted back to that thread...

http://www.mrexcel.com/forum/excel-...lp-visual-basic-applications.html#post3961202

Note, though, that the lists my code could be much larger than what a MessageBox can display, so my code adds a new sheet to the workbook, names the sheet for the Fibonacci Number it calculated to and displays the list on the sheet.

Last edited:

#### sliollio

##### New Member
That works great! Except for just one thing. It is creating a new sheet titled "Fibonacci (whatever number you asked it to input)" but it is still displaying the results on my first page rather than the newly created page.

#### Rick Rothstein

##### MrExcel MVP

That works great! Except for just one thing. It is creating a new sheet titled "Fibonacci (whatever number you asked it to input)" but it is still displaying the results on my first page rather than the newly created page.
Sorry, I pointed you to the function version of my code... here is the link I should have pointed you to... it is a macro which should, in fact, do what I described...

http://www.mrexcel.com/forum/excel-...lp-visual-basic-applications.html#post3961404

#### sliollio

##### New Member
Still having the issue with it displaying the result on the first sheet. It IS creating a new sheet, just not displaying the results on that sheet. Aside from that it works perfectly!

#### Rick Rothstein

##### MrExcel MVP

Still having the issue with it displaying the result on the first sheet. It IS creating a new sheet, just not displaying the results on that sheet. Aside from that it works perfectly!
Hmm! I don't get it.. it works correctly for me (I just tested it).

Last edited:

#### sliollio

##### New Member
Strange! Oh well. I will work around it. Thank you so much!

#### Rick Rothstein

##### MrExcel MVP
Strange! Oh well. I will work around it. Thank you so much!

Just out of curiosity, what version of Excel are you using? My bet is on Excel 2013 which I do not have so I cannot test it there; however, I did just test my code XL2003 and XL2007 and it works fine on these versions of Excel as well.

Last edited:

#### sliollio

##### New Member
2010 and it is on a Mac so perhaps that could be causing some issues.

Replies
6
Views
60
Replies
4
Views
56
Replies
10
Views
102
Replies
15
Views
203
Replies
12
Views
201