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
 
2010 and it is on a Mac so perhaps that could be causing some issues.
Oh, a Mac... interesting... I don't have one of those so I can't do any testing. In looking at my code and re-reading your description, I have something for you to try (this is purely a guess, but it seems like a logical one to me). Add the code line I show in red (or simply copy the entire code and paste it over your existing code) and see if that makes the code work the way I described it should...
Code:
Sub Fibonacci()
  Dim X As Long, Z As Long, N As Long, Carry As Long, PositionSum As Long
  Dim N_minus_0 As String, N_minus_1 As String, N_minus_2 As String, FN As Variant
  N = Format(Application.InputBox("Please enter which Fibonacci Number you want to calculate...", Type:=1), "0")
  ReDim FN(1 To N, 1 To 2)
  If N = 1 Then
    FN(1, 1) = "'1"
    FN(1, 2) = "'1"
  ElseIf N = 2 Then
    FN(1, 1) = "'1"
    FN(1, 2) = "'1"
    FN(2, 1) = "'2"
    FN(2, 2) = "'1"
  ElseIf N > 2 Then
    N_minus_1 = "1"
    N_minus_2 = "1"
    FN(1, 1) = "'1"
    FN(1, 2) = "'1"
    FN(2, 1) = "'2"
    FN(2, 2) = "'1"
    For X = 3 To N
      Carry = 0
      N_minus_0 = Space$(Len(N_minus_1))
      If Len(N_minus_1) > Len(N_minus_2) Then N_minus_2 = "0" & N_minus_2
      For Z = Len(N_minus_1) To 1 Step -1
        PositionSum = Val(Mid$(N_minus_1, Z, 1)) + Val(Mid$(N_minus_2, Z, 1)) + Carry
        Mid$(N_minus_0, Z, 1) = Right$(CStr(PositionSum), 1)
        Carry = IIf(PositionSum < 10, 0, 1)
      Next
      If Carry Then N_minus_0 = "1" & N_minus_0
      FN(X, 1) = X
      FN(X, 2) = "'" & N_minus_0
      N_minus_2 = N_minus_1
      N_minus_1 = N_minus_0
    Next
    Sheets.Add After:=Sheets(Sheets.Count)
    [COLOR=#ff0000][B]Sheets(Sheets.Count).Activate[/B][/COLOR]
    ActiveSheet.Name = "Fibonacci(" & N & ")"
    Range("A1:B" & UBound(FN)) = FN
    Columns("A:B").AutoFit
  Else
    MsgBox "The number you enter must be greater than zero!"
  End If
End Sub
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

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