Maybe this will help, I'm using a table-valued function, but it's the same concept:
Code:
Sub InsertParameterizedData()
With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=DatabaseName;Data Source=ServerName;Use Proc" _
, _
"edure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with colu" _
, "mn collation when possible=False"), Destination:=ActiveCell.Range("A2"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT * FROM DatabaseName.dbo.fnConsolidatedInvoices(" & ActiveCell & "," & ActiveCell.Range("B1") & ")")
.Name = "NameYourQueryTableHere"
.Refresh BackgroundQuery:=False
End With
End Sub
The macro above returns the data from fn.ConsolidateInvoices(@StartDate,@EndDate) where I have replaced the parameter values with the value in the ActiveCell and the cell to the right of it.
It then places the data beginning on the cell one row below the ActiveCell.
I got all this using the Macro Recorder (God's gift to those of us unfortunate enough to work with Excel on a regular basis).
Let me know if you still need any help on this.
You can actually replace the "SELECT *..." with any SQL statement you want, and concatenate the cell addresses of the parameters you want, it's pretty damned cool if you ask me.