今天有狗屎运,居然看到了:https://sites.google.com/site/jrlhost/links/excelcdll
简直是我的救星。基本顺着看下来就能搞定。然后吐槽一句,公司居然是x32的office, I 服了U。目前没有想明白为什么。
代码里面要注意的地方:
Option Explicit
' This file provides an example of calling a C function in a DLL
' It was writen on a computer with Windows 7 64-bit and Excel 2010 64-bit and has been tested on a computer with Windows 7 32-bit and Excel 2010 32-bit
' It is provided as an example only. I make no waranties or guarantees about this file, the associated DLL file, the methods contained within, the steps described here or on my website, or anything else. I am unaware of any problems that could be caused by any of these materials, but as with any other code or computer files you find online, use at your own risk.
' The entire example is based on the example shown http://www.youtube.com/watch?v=x3WWA8eEX9Q, with the addition of the squareOnWorksheet() function and Private keyword in the Declare statement
' Author: Jonathan Lhost (jrlhost@utexas.edu) June 17, 2012
' Declare the function that's in the DLL
Private Declare PtrSafe Function squareForEXL Lib _ '以下这个路径差点让我放弃,一开始就没想过公司office是x32。干得好,又让我虚度光阴片刻钟...
"C:\Users\msong1\Documents\Visual Studio 2010\Projects\SquareDLL\Debug\SquareDLL.dll" (ByRef x As Double) As Double
' Use function on worksheet
Function squareOnWorksheet(dArg As Double) As Double
squareOnWorksheet = squareForEXL(dArg)
End Function
' Example using the function from VBA
Sub useSquareInVBA()
MsgBox squareForEXL(10)
End Sub
棒棒哒~