Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA中插入临时表_Vba_Excel - Fatal编程技术网

如何在VBA中插入临时表

如何在VBA中插入临时表,vba,excel,Vba,Excel,当我运行以下代码时,我收到一个错误,“运行时错误'1004':应用程序定义的错误或对象定义的错误” 选择“调试”时,将突出显示以下行:。刷新背景查询:=False querystr = "SET NOCOUNT ON" & Chr(13) & _ "SELECT CSD.StoreNo AS 'StoreNo',SUM(CSD.Amount) as totalSales " & Chr(13) & _ "INTO #Sales

当我运行以下代码时,我收到一个错误,“运行时错误'1004':应用程序定义的错误或对象定义的错误”

选择“调试”时,将突出显示以下行:。刷新背景查询:=False

querystr = "SET NOCOUNT ON" & Chr(13) & _
        "SELECT CSD.StoreNo AS 'StoreNo',SUM(CSD.Amount) as totalSales " & Chr(13) & _
        "INTO    #SalesOfTheStores " & Chr(13) & _
        "FROM Purchase.dbo.CashsheetDetail as CSD " & Chr(13) & _
        "INNER JOIN Purchase.dbo.CashsheetHeader as CSH on CSH.TransferID = CSD.TransferID and CSH.StoreNo = CSD.StoreNo " & Chr(13) & _
        "WHERE  CSD.Comments = 'Total Gross Sales' AND CSH.DayendDate between '" & StartDate & "' And '" & EndDate & "' " & Chr(13) & _
        "GROUP BY CSD.StoreNo; "



With ActiveSheet.QueryTables.Add(Connection:= _
    "ODBC;DRIVER=SQL Server;SERVER=" & Div & "DBL01\SR;UID=" & User & ";APP=Microsoft Office 2003;WSID=" & PC & ";DATABASE=Purchase;Trusted_Connection=Yes" _
    , Destination:=Range("A1"))
    .RefreshStyle = xlOverwriteCells
    .CommandText = querystr
    .Refresh BackgroundQuery:=False

结束您的查询字符串不会返回数据。将SQL更改为SELECT语句,而不是SELECT INTO(删除INTO子句)。

感谢您的回复,但我想将查询运行到临时表查询表需要数据;如果插入临时表,该表将在SQL数据库中创建,而不会返回Excel。