Interface excel vba - fortran

I wrote a fortran program to calculate the moments and shear forces in a 2-span beam under moving forces.
I first tried with VBA but it took too long to run.
I want to pass the input from excel to fortran and get the results back.
I did some research but there is little to be found on the subject.
I tried with a simple routine i.e. adding two numbers an compiled the fortran source to a DDL. But after that I’m stuck. I found an example program on chat GPT but it doesnt work.
https://chatgpt.com/c/58715b3e-09a3-4d9b-9e5c-db20fe33d567

Assuming that you are only interested in Excel on Windows (and not Mac, Microsoft 365, etc.), I found an older tutorial on using VBA in Office products:

In your VBA script you will need to declare your function:

Public Declare Function getNumber 
    Lib "C:\MyLibs\myfuns.dll" (ByRef A As Long) As Long
// myfuns.c
__declspec(dllexport) int getNumber(int *a) { return *a + 8; }

I’m not 100% sure, but in Fortran you’ll need to use a compiler-specific attribute to make sure the procedure is exported in the DLL:

! myfuns.f90
function getNumber(a) bind(c,name="getNumber")  ! case-sensitive (!)
!GCC$ ATTRIBUTES DLLEXPORT :: getNumber
use, intrinsic :: iso_c_binding, only: c_int
integer(c_int), intent(in) :: a
integer(c_int) :: getNumber
getNumber = a + 8
end function

It looks like Microsoft promotes a new mechanism for programming Add-ins, based on an Excel Javascript API, instead of the older COM and VBA technologies:

I have no idea if C or Fortran can be called via Javascript. The Add-in in this case becomes it’s own web-application… :see_no_evil:

1 Like

You also asked this in the Intel Fortran forum, and I see you have read my response there. I’ll continue it there as well.

Hi @rmoortgat ,
You may find some resources here

Problem solved thanks to Steve Lionel who pointed me to an example.
See his post above.

Roger