Fortran 90 with Excel – gfortran example

Recently i created a program that involved Fortran and excel.  The main solving was handled by the Fortran and excel provided the GUI.

I know many people search for this so here’s the small example that i worked on before going to the main program.

I used gfortran as Fortran compiler and Excel 2003 for the GUI.

Fortran program accepts and array and does some calculation and returns the values back to excel.

The excel VBA code, call the Fortran dll and then the results form the dll call are posted in excel cells.

Here’s the Fortran program

        Subroutine FortranDLL( Array1, upbound )
        Implicit None
! …argument declarations
        Integer :: upbound
        Integer :: Array1(1:upbound)
! Local variables
        Integer :: i
        do i=1,upbound
        Array1(i)=288.16-0.0065*Array1(i)
        end do
        End Subroutine FortranDLL

It was compiled with the following options.

gfortran -mrtd -fno-underscoring -shared -o fortrandll.dll fortrandll.f90

And here’s what I did in the excel.

Declare Sub fortrandll Lib "C:\TEMP\fortrandll.dll" (ByRef Array1 As Double, ByRef upbound As Long)

Sub Button1_Click()
    Dim II As Long
    Dim test(10) As Double
    II = 11
    Call fortrandll(test(1), II)
    Range("a1").Value = test(1)
    Range("a2").Value = test(2)
    Range("a3").Value = test(3)
    Range("a4").Value = test(4)
    Range("a5").Value = test(5)
    Range("a6").Value = test(6)
    Range("a7").Value = test(7)
    Range("a8").Value = test(8)
    Range("a9").Value = test(9)
    Range("a10").Value = test(10)
End Sub

Will post more in coming weeks. Maybe a full fledged excel application using a fortran dll.

About these ads

Tagged: ,

7 thoughts on “Fortran 90 with Excel – gfortran example

  1. Scott Palmtag February 8, 2011 at 7:42 pm Reply

    I think ARRAY1 should be declared double precision in the Fortran subroutine (not integer).
    Thanks for the example!

  2. [...] Fortran 90 with Excel – gfortran example [...]

  3. [...] How to create fortran DLL with gfortran open source compiler? Share this:ShareTwitterLinkedInFacebookEmailLike this:LikeBe the first to like this post. from → coding, excel, fortran, technical, vba ← April is the cruellest month and 3 hat-tricks No comments yet [...]

  4. 2012 in Review « SukhbinderSingh.com January 4, 2013 at 4:19 pm Reply

    [...] Fortran 90 with Excel – gfortran example [...]

  5. Top 5 posts | SukhbinderSingh.com July 16, 2013 at 10:16 pm Reply

    […] Connecting Gfortran and ms excel via a fortran dll […]

  6. […] Fortran 90 with Excel – gfortran example is one of the post that constantly gets lot of attention as measured by the top posts sidebar widget on this blog. […]

  7. Sybil May 22, 2014 at 1:22 am Reply

    I like what you guys are usually up too. Such clever work and exposure!
    Keep up the fantastic works guys I’ve included you guys to my own blogroll.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 966 other followers

%d bloggers like this: