You are on page 1of 3

How to Call a FORTRAN DLL from VBA (FORTRA...

http://www.emagenit.com/FORTRAN DLL.htm

How to Call a FORTRAN DLL from VBA (FORTRAN DLL Overview)


Call us at: 1.805.498.7162 1.805.558.9277--

FORTRAN DLL Overview


The concept of communicating between procedures written in more than one language, in this case FORTRAN and VBA is alien to most designers. There are many advantages however to communicating with procedures written in other languages. The main reason is a programmer does not have to rewrite verified code VBA communicates with FORTRAN procedures by compiling FORTRAN source files, object files, static libraries...etc into what are called Dynamic Link Libraries or FORTRAN DLLs for short. A FORTRAN DLL resembles a FORTRAN static library; the main difference being the DLL needs an interface specification and associates itself with a project during execution, not during linking. VBA procedures call and execute FORTRAN procedures contained in a FORTRAN DLL. Externally declared Subroutine and Function procedures inside a FORTRAN DLL are the only procedures that can be called and executed by a VBA procedure. The definition of external means the Subroutine or Function procedure is not defined within an another program unit in the FORTRAN DLL. A FORTRAN DLL shares its code and data address space with the application calling it. FORTRAN DLL's offer the following advantages: The organizational advantage of a static library, which means it is not included in your VBA projects code. This means the workbook / project is smaller and takes up less memory when open in Excel. Smaller executable file Object code inside the FORTRAN DLL is associated in a dynamic manner with the calling procedure when executed. This means if a DLL contains 400 procedures, only the object code needed for the current routines are utilized. More than one application can access a DLL at a time. For a VBA procedure to execute a FORTRAN DLL Subroutine or Function procedure, VBA must first know: Where to find the FORTRAN DLL. What type of FORTRAN DLL procedure will be executed and the name of the procedure. The FORTRAN DLL procedures dummy arguments and their data types. The statement that declares this information to VBA is called a Declare statement. It is placed at the top of a VBA projects module in its declaration section. FORTRAN DLL's can be run from both VBA Sub procedures and Function procedures. Which means you can run a 10,000 line numerical analysis routine from a single worksheet cell. Think of the possibilities.

How VBA Procedures Communicate with a FORTRAN DLL


VBA procedures call and execute FORTRAN DLL Subroutine and Function procedures by Call statements. The call statements resemble any other call statement in VBA. FORTRAN DLL Sub procedures are called like VBA subs FORTRAN DLL Function procedures are called like VBA functions A central concept about this calling strategy is a VBA procedure can run a FORTRAN DLL procedure, but a FORTRAN DLL procedure cannot in turn run a VBA procedure.

1 sur 3

24/12/2012 17:49

How to Call a FORTRAN DLL from VBA (FORTRA...

http://www.emagenit.com/FORTRAN DLL.htm

This strategy in mind, VBA procedures can be thought of as pre/post information processors for FORTRAN DLL routines. Excel in theory becomes the GUI dashboard that FORTRAN never had
VBA procedures know how to call a FORTRAN routine via a Declare statement. AN Example Declare statement is presented below.

Declare Sub FinVel Lib C:\Project\EngPrc.DLL (m1 As Double, v1i As Double, m2 As Double, vf As Double)
The name after Sub defines the procedure to call in FORTRAN, Lib is the path to the DLL and the ( )'s are its argument list.

How to Call a FORTRAN DLL from VBA (Coding Example)


VBA Declare statement that identifies the FinVel SubRoutine in the EngPrc _ FORTRAN DLL. Declare Sub FinVel Lib C:\Project\EngPrc.DLL (m1 As Double, v1i As Double, _ m2 As Double , vf As Double)

Sub Velocity( ) Procedure gathers info for FinVel and outputs its calculations. Dim m1 As Double, v1i As Double, m2 As Double, vf As Double m1 = Range(B2).Value Read m1 mass value (kg) from cell B2 on active worksheet. v1i = Range(B3).Value Read velocity of m1 (m/s) from cell B3 on active worksheet. m2 = Range(B4).Value Read m2 mass value (kg) from cell B4 on active worksheet. Call FinVel(m1, v1i, m2, vf) Call FORTRAN FinVel Subroutine procedure. Use arguments m1, v1i, m2, vf to pass information to FORTRAN DLL procedure. Use vf to pass back answer. Range(B7).Value = vf Set cell B7 value on active worksheet to vf. End Sub

!FORTRAN Subroutine; Calculates final velocity of m1m2 collision (m/s). Subroutine FinVel(m1, v1i, m2, vf) Real(8) m1, v1i, m2, vf !MS$ ATTRIBUTES DLLEXPORT:: FinVel !MS$ ATTRIBUTES ALIAS: FinVel:: FinVel !Final velocity for both masses (stuck) vf = m1 * v1i / (m1 + m2) !Expression calculates final velocity of m1m2 (m/s). End Subroutine

Additional Pages Microsoft Excel Training Online Microsoft Excel Training Microsoft Excel Classes Microsoft Excel Courses Microsoft Excel Seminars Microsoft Excel Macros Microsoft Excel VBA Consulting Microsoft Excel Add-Ins
Copyright 2002-2012 EMAGENIT All Rights Reserved

2 sur 3

24/12/2012 17:49

How to Call a FORTRAN DLL from VBA (FORTRA...

http://www.emagenit.com/FORTRAN DLL.htm

3 sur 3

24/12/2012 17:49