您好我正在尝试使用以下代码将此查询的结果打印到Excel文档中的其他选项卡,因此我可以在摘要页面上有一个按钮,并将结果导出到特定选项卡。导入到选项卡上的所有内容都可正常工作,但上面有宏,但不想打印到单独的工作表。任何帮助都会很棒!
Cells.Select
Range("A3").Activate
Selection.ClearContents
Range("A3").Select
ActiveWorkbook.Queries.Add Name:="Query1" & Now, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(Web.Contents(""website""),[Delimiter="","", Columns=15, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " " & _
" #""Promoted Headers"" = Table.PromoteHeaders(Source)," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Issue Type"", type text}, {""Custom field (Epic Name)"", type text}, {""Summary"", type text}, {""Assignee"", type text}, {""Reporter"", type text}, {""Status"", type text}, {""Resolution"", type text}, {""Created"", type datetime}, {" & _
"""Updated"", type datetime}, {""Due Date"", type datetime}, {""Component/s"", type text}, {""Component/s_1"", type text}, {""Component/s_2"", type text}, {""Custom field (Status Update)"", type text}, {""Custom field (Epic Link)"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1" _
, Destination:=Range("$A$2")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Query1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Query1"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Application.CommandBars("Workbook Queries").Visible = False
While ActiveSheet.QueryTables.Count
ActiveSheet.QueryTables(1).Delete
Wend
End Sub
答案 0 :(得分:1)
假设您要将结果打印到Sheet2,您可以尝试更改它:
, Destination:=Range("$A$2")).QueryTable
到此:
, Destination:=Sheet2.Range("$A$2")).QueryTable