Topic: Fortran from Excel

Your blogs 4 and 5 describe how to call Fortran from Excel via Visual Basic. To me, a novice, there seem to be gaps in these. These are old blogs However, I’ve got to these routines to average to numbers, which compile ok.

MEAN.FOR as follows:

Module MEAN_MODULE
    USE ISO_C_BINDING
    IMPLICIT NONE
CONTAINS
   FUNCTION MEAN(A,B) BIND(C, name=“mean”) RESULT (M)
      REAL(C_DOUBLE), VALUE :: A, B
      REAL(C_DOUBLE) M
      M = (A + B)/2.
   END FUNCTION
END MODULE


MEAN.DEF as follows:

LIBRARY MEAN
EXPORTS
   MEAN=“MEAN@16”

This gets … Generating Makefile Okay
But then ….  ld returned 1 exit status
               Last command making (target.exe) returned in bad status.

Any clue as to what I am missing, please.

Re: Fortran from Excel

I can see a few problems in your post.  First, your function that you want to have callable from a DLL is inside a Fortran module.  Using a module to encapsulate DLL-exported functions really won't work well since the compiler is not generating a simple name for the function.  You want to move the function outside the module so it is standalone:

   FUNCTION MEAN(A,B) BIND(C, name=“mean”) RESULT (M)
      REAL(C_DOUBLE), VALUE :: A, B
      REAL(C_DOUBLE) M
      M = (A + B)/2.
   END FUNCTION

Second, the blog's instructions were for generating a 32-bit DLL.  There's a good chance that, hopefully, you're using 64-bit Excel, which simplifies everything greatly.  Specifically, the export definition where it adds the "standard call" decoration of @16 is no longer necessary.  You probably don't need to create an export definition file at all if you're in 64-bit mode.

Third, when you build your library, you'll notice that the compiler is attempting to make an executable named "target.exe."  It's almost certainly failing because your code does not contain any program unit.  You'll need to ensure you're doing two things:

1. Use a Project - You need to create a new project (Shared Library (DLL)) by selecting "New Project" in the Project menu.  After you set up and select a reasonable location for your project, possibly the same directory where your Fortran source code is, you need to add your Fortran source code to the project (again, in the Project menu, select "Add File(s)") and save the project.

2. Configure the Project - In Project Options, make sure that the Architecture is selected to match your Excel installation's architecture (32- or 64-bit) and the project type is set to "Shared Library."  Save your project after you make any changes.

After you build a DLL, you should be able to access it via Excel's Visual Basic for Applications interface.  However, accessing a DLL from Excel isn't a simple process.  It is actually quite complicated.  You'll need to make sure the Excel VBA code has the correct path to the DLL and that the function's signature is correct.  The process gets harder once you start passing arrays to Fortran.  It is completely possible, but there is a learning curve here that is significant.  I just wanted to warn you.

Jeff Armstrong
Approximatrix, LLC