我在一个单元格中有这个公式:
=GetData("Channel_01","Chicago")
执行此代码:
Public Function GetData(ChannelCode As String, Key As String) As String
Dim sql As String
Dim cmd As New ADODB.Command
Dim outputTo As Range
Set outputTo = Application.Caller
sql = "select * from ChannelData WHERE ChannelCode = ? AND Key1 = ?"
Set cmd = getCommand(sql, ChannelCode, Key)
Dim rs As ADODB.Recordset
Set rs = cmd.Execute
WritePivotRecordset ChannelCode, rs, outputTo.Offset(1, 0)
End Function
Public Sub WritePivotRecordset(ChannelCode As String, rs As ADODB.Recordset, destination As Range)
Dim i As Integer
'*** WHEN THIS LINE OF CODE IS REACHED AND EXECUTES, PROCESSING STOPS
Set destination.Value = ChannelCode
For i = 1 To rs.Fields.Count - 1 'skip first field
destination.Offset(0, i).Value = rs.Fields(i).Name
Next
destination.Offset(1, 0).CopyFromRecordset rs
End Sub
问题出现在这一行:
'*** WHEN THIS LINE OF CODE IS REACHED AND EXECUTES, PROCESSING STOPS
Set destination.Value = ChannelCode
是否设置此调用电子表格重新计算,终止正在执行的 VBA 线程或类似的东西?我是这么想的,所以我在写任何输出之前尝试了这个:
Application.Calculation = xlCalculationManual
但现在在同一行代码上我得到: 应用程序定义或对象定义错误。
是否从 VBA 函数写入调用 VBA 函数的同一个工作表,只是不允许?