#VAL!尝试使用 DLL 从 excel 调用简单的 C++ 程序时的答案

#VAL! answers when trying to call a simple c++ program from excel with DLL

我对 C++ 很陌生,对创建 DLL 文件也很陌生。作为训练练习,我尝试创建一个简单的 excel 函数,将输入数据乘以 12。但是 excel 不断抛出值错误。

我想我遇到了类似于 using C++ 2010 dll in excel 的问题。我按照 www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf 中的说明创建代码,当它不起作用时,我进一步简化了它。

我的 cpp 文件是:

#include "stdafx.h"

using namespace std;

static int  _stdcall Multiply(int x)
{
    int a = 12;
    return a*x;
}

我的 DEF 文件是:

LIBRARY DLLtest
EXPORTS
Multiply

最后我的 VBA 代码是:

Declare Function Multiply _
Lib "C:\Users\[path]\Debug\DLLtest.dll" _
(ByVal x As Integer) As Integer

编辑:我应该补充一点,我使用的是 excel 2010 和 visual studio express 2013。C++ 代码还附带了一些 dllmain.cpp 和 [=30 中的预构建代码=] 但我假设这不是问题所在。

原来你必须更改 compiler/project 中的选项才能使其理解如何使用 def 文件和 DLL。 This site 给出了很好的解释。