Problems in VBA libraries

Problems in VBA libraries


I am working with libraries to VBA Excel 2010 (xlsm format), but I have problems in the excel ouputs. This is my script:

* fortran 11

    Subroutine FortranDLL( Array1, upbound )

    Implicit None


    Integer :: upbound

    Integer :: Array1(1:upbound)

     Integer :: i

    do i=1,upbound


    end do

    End Subroutine FortranDLL


* visual basic macro


Option Explicit

Declare Sub fortrandll Lib "fortrandll.dll" (ByRef Array1 As Double, ByRef upbound As Long)


Sub Button1_Click()


Dim II As Long

Dim test(10) As Double


ChDrive (ThisWorkbook.Path)

ChDir (ThisWorkbook.Path)


II = 11

Call fortrandll(test(1), II)

Range("a1").Value = test(1)

Range("a2").Value = test(2)

Range("a3").Value = test(3)

Range("a4").Value = test(4)

Range("a5").Value = test(5)

Range("a6").Value = test(6)

Range("a7").Value = test(7)

Range("a8").Value = test(8)

Range("a9").Value = test(9)

Range("a10").Value = test(10)

End Sub


The problem is that the output is “0” vector and it isn’t the solution.

Someone know what is wrong?

What is the solution?




6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Try these changes:

Declare Sub fortrandll Lib "fortrandll.dll" (ByVal Array1() As Long, ByRef upbound As Long)
Call fortrandll(test, II)

The way you're doing it was often recommended in the past, but it's wrong. Also you told VB that the argument was Double but you declared it in Fortran as integer. Make them consistent.

Retired 12/31/2016

(Array arguments must be ByRef in VBA. I think the old way is still *the* way, unless you want to play with SafeArrays.)

The old way doesn't work in newer VBs and with x64. See the VB-Calls-Fortran sample we provide.

Retired 12/31/2016

The variant of VB used as the macro language in Excel 2010 (i.e. VBA) is still akin to VB 6.0, with some additions (LongPtr) for 64 bit office. Consequently "ByVal array_argument()" won't compile. The distinction between VBA and VB is important - there was a thread here a few months back where I went off in a flat panic until I was reminded of this.

(I've not used versions of office later than 2010. Perhaps things have changed further.)

Ian, you're right - VBA is not VB. I am sure you have a lot more experience in this area than I do.

Perhaps it was just the use of double instead of long that was the problem here.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today