fortran excel vba

Gary Davies

New Member
Joined
Jul 28, 2003
Messages
8
I am willing to hire someone for a few hours of help if necessary. I'm tired of beating my head and getting nowhere. I need to get this working so I can do productive work.

I need to get some simple example fortran program functions working as functions I can call from excel or an excel macro. Once the examples work, I should be able to take it from there with more complex programs. I am using gfortran as my compiler, Windows Vista, excel 2003. I have successfully gotten the integer math function "intadd" working but the real number function "numadd" gives me a "2 E308" answer when I use the function in excel.

fortran file test1.f95:
function intadd(i,j)
intadd=i+j
end

fortran file test2.f95:
function numadd(a,b)
real*8 a,b
numadd=a+b
end

batch program make1.bat to compile test1.f95:
gfortran -s -shared -mrtd -o test1.dll test1.def test1.f95

batch program make2.bat to compile test2.f95:
gfortran -s -shared -mrtd -o test2.dll test2.def test2.f95

excel vba macro:
Declare Function intadd Lib "c:\g77\test1.dll" (i As Long, j As Long) As Long
Declare Function numadd Lib "c:\g77\test2.dll" (a As Double, b As Double) As Double

Function gdfct1(i, j)
gdfct1 = intadd(i, j)
End Function

Function gdfct2(a, b)
gdfct2 = numadd(a, b)
End Function

The gdfct1 works correctly when called from a cell in the worksheet, as does putting "=intadd(3,4)" in a cell in the worksheet.
The gdfct2 gives me a "#value" error in the worksheet, and putting "numadd(4.4,5.5)" gives me the "2 E308" value.

I've tried upper case function names and a few other things I've read on line. Doesn't seem to be a path issue since the intadd function works fine. Maybe variable declaration types, which I am not good with?

thanks for any help you can give me.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This is how I would code them (no need for the Declare Function statements)

Code:
Function intadd(i As Long, j As Long) As Long
intadd = i + j
End Function
End

Function numadd(a As Double, b As Double) As Double
numadd = a + b
End Function
 
Upvote 0
Peter, thanks for the comment but I'm not trying to figure out how to do this in an excel macro, which is what I think you are showing me. I need to build functions in fortran that can be accessed in excel via a DLL interface. The examples I gave are trivial cases. Once I figure out how to do this, I have large fortran programs that I will use.

thanks
 
Upvote 0
Gary

Could the problem with what you are trying perhaps be caused by not using the correct data types for the parameters for either the VBA function
or the call to the DLL function?

You aren't typing any of them.

That might be ok with a datatype like an integer, perhaps there is some conversion going on.

When it comes to doubles etc they way they are being stored might not be compatible across VBA and FORTRAN.

I hope that makes some kind of sense, I suppose it could be regarded as some sort of coercion or something.

Or I could be talking nonsense.:)

eg
 
Upvote 0

Forum statistics

Threads
1,215,549
Messages
6,125,473
Members
449,233
Latest member
Deardevil

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