Re: Using fortran code with VBA
- From: e p chandler <epc8@xxxxxxxx>
- Date: Fri, 29 Jun 2007 19:57:45 -0700
On Jun 29, 8:59 pm, veda <kedar.hardi...@xxxxxxxxx> wrote:
Hi,
I have done limited programming with both VBA and FORTRAN (CVF) i.e.
separately. I would like to know how to use VBA for front end while
you use your (existing) fortran code in the background.
If someone can tell me steps to follow for creating simplest project
that illustrates the process, I would appreciate that. I am not
familiar with creating DLLs etc. So, if that is required, I would need
pointers/help for that.
The line of help I am seeking is something like this -
1. Lets say you have a cell in excel where user enters a number
2. Create a "button" for the user to hit that invokes VBA code
"mycode"
3. "mycode" is a VBA code that reads the "input" from the cell and
sends it to a fortran code say "ComputeSquare" e.g. compute the square
of the number (of course, this doesn't need fortran code interface
etc..) and somehow returns to VBA code "mycode"
4. "mycode" then puts that value (squared) in another cell in excel
for user as output.
What I do not know is -
1. What do I need to do after creating "ComputeSquare" in CVF the
usual way so that it can be accessed from VBA? Can "ComputeSquare" be
just a fortran subroutine or is it a full fortran program with its
"main" etc? If it is a DLL, please clarify how do I go about creating
and using it.
2. How do you call "ComputeSquare" (either a full program or a
subroutine) from within VBA and send in the input/output across?
Any help will be greatly appreciated.
Veda.
See my other reply for general principles.
Here is my recipe for creating a DLL with g95 that contains a function
that takes the difference of two 32 bit integers and returns their
difference:
test.f is the code file, test.def handles exports and name mangling,
make.bat creates the DLL using g95. The various switches on the
command line are explained elsewhere in some of my previous posts.
test.f:
function isub2(i,j)
isub2=i-j
end
test.def:
EXPORTS isub2=isub2_
make.bat:
g95 -s -shared -mrtd -o test.dll test.def test.f
running make.bat leaves you with the file test.dll
put the DLL in a directory on the PATH or in one of
the directories that contains system DLLs
In Excel:
under tools / menu / visual basic editor
under insert / module
in module (general) (declarations) insert
Declare Function isub2 Lib "test.dll" (i As Long, j As Long) As Long
in a cell insert the formula
=isub2(a1,b1)
save the work***
re-open it and enable macros
type something into cells a1 and b1
The changes and steps you need to take with CVF are clearly documented
elsewhere. IIRC you need to add a comment line which contains
directives to the compiler. These specify the calling convention, what
functions to export and what name alias to use.
-- elliot
-- e-mail: epc8 at juno dot com
.
- References:
- Using fortran code with VBA
- From: veda
- Using fortran code with VBA
- Prev by Date: Re: Need Help: compiling old Fortran program
- Next by Date: Re: Form k = i + j and test for overflow.
- Previous by thread: Re: Using fortran code with VBA
- Index(es):