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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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
 
Upvote 0
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:
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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!
 
Upvote 0
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:
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,118
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top