Building a DLL for use in Excel

Suppose I have a function written in C++, say

double GenerateRandomNumber(double seed)

I want to compile it as a DLL and attach it to an Excel spreadsheet, so I can use it like an ordinary VBA function. My questions:

1. How do I change the settings in Visual Studio so that the code gets compiled as a DLL?
2. How do I use the DLL in Excel?
RothbardAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
BTW, more sample code here:

http://www.codeproject.com/KB/macros/InterpolationAddin.aspx ("Sample Excel Function Add-in in C")
http://www.codeproject.com/KB/macros/easyif.aspx ("Excel Add-in Using Excel C API and COM")
0
 
jkrCommented:
Check out http://support.microsoft.com/kb/178474 ("How To Build an Add-in (XLL) for Excel Using Visual C++ ")
0
 
RothbardAuthor Commented:
Is there any way of doing this without having to buy the book "Microsoft Excel 97 Developer's Kit"?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RothbardAuthor Commented:
By the way, I tried to load the XLL file provided on the Code Project website, but it didn't work in my copy of Excel 2003. I get the error "Interp32.dll is not a valid add-in".
0
 
RothbardAuthor Commented:
Is there a way of calling a DLL from Excel without actually making an Add-In out of it?
0
 
RothbardAuthor Commented:
I forgot to add that I don't need to use the DLL in an Excel sheet, it's enough to be able to use it in VBA.
0
 
jkrCommented:
>>Is there a way of calling a DLL from Excel without actually making an Add-In
>>out of it?

I strongly doubt that, even from VBA. Except maybe if you can call COM objects from VBA, them that would be an option.
0
 
RothbardAuthor Commented:
So if I make it a COM DLL I should be able to call it from VBA? How can I do that?
0
 
jkrCommented:
WeGood question. I don't even know VBA well enough to know if that is possible at all...
0
 
Rory ArchibaldConnect With a Mentor Commented:
Set a reference to it in your VBA Project, then you should be able to use its methods and objects etc.
Regards,
Rory
0
 
RothbardAuthor Commented:
How do I set the reference? I don't have a project by the way, only a module in an Excel spreadsheet.
0
 
Rory ArchibaldCommented:
Every workbook contains/is a project. You set a reference via the Tools->References option of the menu. You will need to browse to your dll.
Rory
0
All Courses

From novice to tech pro — start learning today.