【发布时间】:2013-08-02 00:53:14
【问题描述】:
请帮我在 excel vba 中创建一个用户定义的函数 例子
Function GetTheValue(wbPath, wbName, wsName, cellRef)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim tmp As Range
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & wbPath & wbName & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
rst.Open "SELECT * FROM [" & wsName & "$" & cellRef & "]", cnn
Set tmp = Range("L5")
tmp.CopyFromRecordset rst
MsgBox tmp.Value
GetTheValue = tmp.Value
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Function
我尝试通过签署公式在单元格中使用它
=GetThaValue("D:\";"test.xls";"Sheet1";"B4")
并看到我的代码的字符串“tmp.CopyFromRecordset rst”不起作用 请你能帮我解决这个问题。 非常感谢
【问题讨论】:
标签: excel vba excel-formula ado user-defined-functions