在excel中,我想编写如下函数:
Function GetDCF(vari As String)
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim strConn As String
Dim sql As String
strConn = "PROVIDER=xxxx;DATA SOURCE=xxxxx;INITIAL CATALOG=Research;xxxxx SECURITY=sspi;"
cn.Open strConn
sql = "select '" & vari & " from TBLmontecarlodcf where COMPANY = '" & Range("A2").Value & "' "
rs.Open sql, cn, adOpenDynamic, adLockOptimistic
GetDCF = rs!vari
cn.Close
Set rs = Nothing
Set cn = Nothing
End Function
对于行GetDCF = rs!vari
,我知道代码不正确,但我不知道如何修复它?
它应该是GetDCF = rs!'" & vari & "
吗?
基本上,我不确定如何将SQL查询中的值传递给我的VBA函数。
感谢您的建议!