Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Visual Studio 2019 C++ dll Excel VBA addin Problems

$
0
0

I am having problems getting a simple Excel VBA addin working. I am using the simple example at https://www.vitoshacademy.com/c-adding-c-function-to-vbaexcel/ to get started, but can't get even this simple example working. SimpleMath.cpp, defMe.def, and VBA code is below. I have added to the SimpleMath.cpp properties: Linker: Input: Module Definition File: defMe.def. In VS2019, the C++ code successfully compiles and produces SimpleMath.dll as expected in the Debug folder. But I have tried many alternatives, but can't get the VBA code in Excel to work. As the many web variations of the example appear old, I am wondering if VS2019/C++ requires something not covered in the old examples?

Guidance and suggestions will be appreciated.

//SimpleMath.cpp
int __stdcall SimpleMath(int & x)
{
    int result = 0;
    for (int a = 0; a <= x; a++) {
        result += a;
    }
    return result;
}
// defMe.def C++ code
LIBRARY "SimpleMath"
EXPORTS
SimpleMath
// VBA Code in Excel Module
Declare Function SimpleMath Lib "C:\VS2019Projects\SimpleMath\Debug\SimpleMath.dll" (ByRef x As        Long) As Long
Sub TestMe()
    Dim n As Long: n = 5
    dim result as long
    result = SimpleMath(n)
End Sub

Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>