Excel SQL导入 – 错误3704closures对象时不允许操作

当试图在Excel模型中执行一个子我收到一个错误消息说:

对象closures时不允许操作。

我已经写了这样的代码,以便从SQL数据库导入数据,从来没有任何问题。 我猜这个问题是sql查询正在做一个临时表,从存储过程的结果,其中我只想要4个最新的数据点。

子看起来像这样:

Sub sqlImport() Dim dbConn As ADODB.connection Set dbConn = New ADODB.connection dbConn.ConnectionString = "Provider=SQLOLEDB;Integrated Security=SSPI;Network=dbmssocn;Initial Catalog=Funds;Data Source=" & Data_Source_Prod02 dbConn.CursorLocation = adUseServer dbConn.Open 'Define cell - top left in table Dim startCell As Range Set startCell = Import.Range("tableTopLeft") 'Clear the table Range(startCell, startCell.End(xlDown).End(xlToRight)).ClearContents 'Setup SQL import Dim CmdSP As New ADODB.Command CmdSP.CommandType = adCmdText CmdSP.CommandText = "CREATE TABLE #tmpBus2 (FundID INT, PortfolioDate date, TotalFundValueBillionSEK float, NAVSEK float, FundShares float)" & _ "INSERT INTO #tmpBus2 EXEC Funds.Analysi.Fund_Size @FundId = 4235 " & _ "SELECT TOP 4 PortfolioDate, NAVSEK, FundShares FROM #tmpBus2 ORDER BY PortfolioDate DESC" CmdSP.ActiveConnection = dbConn 'Execute command Dim dbList As ADODB.Recordset Set dbList = CmdSP.Execute 'Print table data Dim row As Integer, col As Integer row = 1 While Not dbList.EOF For col = 0 To dbList.Fields.Count - 1 startCell.Offset(row, col) = dbList(col) Next col dbList.MoveNext row = row + 1 Wend dbConn.Close End Sub 

错误发生在

 While Not dbList.EOF 

任何人都有解决scheme?

我不能重写存储过程。 我从存储过程得到的结果是相当大的数据集,这就是为什么我只想select前4行。