Fibonacci Calculator

sliollio

New Member
Joined
Sep 8, 2014
Messages
22
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
 

Some videos you may like

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
Joined
Apr 25, 2006
Messages
19,825
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
Joined
Apr 18, 2011
Messages
36,307
Office Version
  1. 2010
Platform
  1. Windows
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
Joined
Sep 8, 2014
Messages
22
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
Joined
Apr 18, 2011
Messages
36,307
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

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
Joined
Sep 8, 2014
Messages
22
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.:confused: Aside from that it works perfectly!
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,307
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

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.:confused: Aside from that it works perfectly!
Hmm! I don't get it.. it works correctly for me (I just tested it).
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,307
Office Version
  1. 2010
Platform
  1. Windows
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:

Watch MrExcel Video

Forum statistics

Threads
1,108,787
Messages
5,524,877
Members
409,609
Latest member
Channingz

This Week's Hot Topics

Top