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

Reading from and Writing to Excel Using Fortran and VBA

adamanthaea
Beginner
738 Views
I am attempting to write a program that will take a set of values from Excel and have the math done in Fortran, using VBA as the intermediate. In an attempt to figure this out, I wrote a small VBA program that will take the values of cells and then perform operations, outputing to a second set of cells. The VBA code is:


Private Sub CommandButton1_Click()
Dim i As Integer, imax As Integer, imax1 As Integer
Dim x As Single, y As Single
imax = Sheet1.Cells(1, 1) ' Number of first type of operation to perform
imax2 = Sheet1.Cells(1, 2)
0 Kudos
2 Replies
adamanthaea
Beginner
738 Views
Whoops, I'm not sure what happened. Must've accidentally tabbed to the submit message. If I can try this again:

I am attempting to write a program that will take a set of values from Excel and have the math done in Fortran, using VBA as the intermediate. In an attempt to figure this out, I wrote a small VBA program that will take the values of cells and then perform operations, outputing to a second set of cells. The current VBA code, which does everything needed at the moment, is:


Private Sub CommandButton1_Click()
'Square a real number
Dim i As Integer, imax As Integer, imax1 As Integer
Dim x As Single, y As Single
imax = Sheet1.Cells(1, 1) 'Number of first type of operation to be performed
imax2 = Sheet1.Cells(1, 2) 'Number of second type of operation to be peformed
For i = 1 To imax
x = Sheet1.Cells(2, i)
y = x ^ 2
Sheet1.Cells(3, i) = y
Next i
Call subx(imax, imax2)
End Sub

Private Sub subx(imax, imax2)
'Convert to integer, then square as an integer
Dim x As Integer, y As Integer
For i = imax + 1 To imax2
x = Int(Sheet1.Cells(2, i))
y = x ^ 2
Sheet1.Cells(3, i) = y
Next i
End Sub

I figure that what I need to do is to create a Fortran DLL with both subroutines included and then have the VBA call the Fortran subroutines and then put the numbers into Excel. However, I'm getting stuck on how to translate the Sheet1.Cells code into what I need for a Do loop. Is there a good way to do this?
0 Kudos
anthonyrichards
New Contributor III
738 Views
0 Kudos
Reply