Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling a Function from Fortran DLL with Excel VBA

Y__G_
Beginner
1,582 Views

Hello, Everyone.

I am a newbie to VBA calling Fortran DLL.

I need some help getting my Fortran DLL file to work with my Excel VBA. 

I am using Windows 7 (X64), Excel 2013 (X64), Microsoft Visual Studio 2015 and  Visual Fortran Composer XE 2013 SP1.

【My FORTRAN code】

Function IntelRho(pi,ti)
    !DEC$ ATTRIBUTES STDCALL,DLLEXPORT::IntelRho
    !DEC$ ATTRIBUTES ALIAS:'IntelRho'::IntelRho

    implicit real(8) (A-H,O-Z)
    real(8) pi,ti,p,t,Intelrho
    p=pi*1000
    t=ti + 273.15
    IntelRho=p/(8.31*t)
    return
End Function Intelrho

【My VBA code】

Public Declare PtrSafe Function Intelrho Lib "D:\Doc\VS2015\Dll2IntelRho\x64\Debug\Dll2IntelRho.dll" (pp As Double, tt As Double) As Double

----------------------------------------------------------------------------------------------------------

When I call the function in an Excel cell, such as: I put 3 in Cells(1,1), put 10 in Cells(1,2), then I call the function IntelRho in Cells(1,3) as "=IntelRho(A1,B1)", I got a wrong answer(Cells(1,3)=0). Could anyone tell what the problem is?

Any help is appricated.

Guo

0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,582 Views

I think you need to add ",REFERENCE" after STDCALL. Have you debugged this to see which values come through to the function?

0 Kudos
Y__G_
Beginner
1,582 Views

Steve Lionel (Intel) wrote:

I think you need to add ",REFERENCE" after STDCALL. Have you debugged this to see which values come through to the function?

Thanks for Mr. Lionel's reply! I can see you all around the forum answering all types questions, I think you must be an enthusiastic expert.

I have debugged the DLL as what you said, but when I repeat the process as aforementioned, Excel got crashed and stopped to work, which hinted me about AppCrash event with the Dll2IntelRho.dll.
【My FORTRAN code】

Function IntelRho(pi,ti)
    !DEC$ ATTRIBUTES STDCALL,REFERENCE,DLLEXPORT::IntelRho
    !DEC$ ATTRIBUTES ALIAS:'IntelRho'::IntelRho
    implicit real(8) (A-H,O-Z)
    real(8) pi,ti,p,t,Intelrho
    p=pi*1000
    t=ti + 273.15
    IntelRho=p/(8.31*t)
    return
End Function Intelrho

【My VBA code】
Public Declare PtrSafe Function Intelrho Lib "D:\Doc\VS2015\Dll2IntelRho\x64\Debug\Dll2IntelRho.dll" (pp As Double, tt As Double) As Double
----------------no any other statement or code in the VBA.----------

Would you like to sending me some example about Excel VBA calling functions from DLL coded by Intel Fortran?
Thank you very much for your attention!

0 Kudos
Steven_L_Intel1
Employee
1,582 Views

In the MixedLanguage.zip sample archive that is installed with the compiler you can find a VB_Calls_Fortran sample that is very similar to what you are doing, other than it uses Visual Basic itself rather than VBA from Excel, but the concepts are the same. I suggest you look at that to see how it works.

0 Kudos
Y__G_
Beginner
1,582 Views

Steve Lionel (Intel) wrote:

In the MixedLanguage.zip sample archive that is installed with the compiler you can find a VB_Calls_Fortran sample that is very similar to what you are doing, other than it uses Visual Basic itself rather than VBA from Excel, but the concepts are the same. I suggest you look at that to see how it works.

Hi, Mr. Lionel, I am glad to see your reply.

I have tried the method as exactly as the MixedLanguage.zip, but I still got the incorrect answer as aforementioned in my previous reply. Could you mind analyzing my problem? Thanks for your time!

0 Kudos
Steven_L_Intel1
Employee
1,582 Views

If you would like me to look at the problem, please attach a ZIP of your VS solution folder with Fortran and Excel files included.

0 Kudos
Reply