如何在新书中创建新的Power Query? Excel VBA

时间:2018-06-19 16:22:26

标签: excel vba excel-vba excel-2016

我用宏录制器录制了宏。 它将在文件本身中创建一个新的Power Query表。 Power Query表绑定到此文件中的一个简单表。 有必要进行制作,以使新书带有Power Query表,并将其附加到第一个文档中。 希望对您有所帮助。

Sub NewCustomer()

'PQ TABLE CREATED

    ActiveWorkbook.Queries.Add Name:="JOURNAL (5)", Formula:= _
         "let" & vbCrLf & "    Source = Excel.CurrentWorkbook(){[Name=""JOURNAL""]}[Content]," & vbCrLf & "    #""Changed Type"" = Table.TransformColumnTypes(Source,{{""DATE"", type datetime}, {""ORDER"", type text}, {""ORDER TYPE"", type text}, {""KASSA N "", Int64.Type}, {""INVOICE N"", Int64.Type}, {""INVOICE TYPE"", type text}, {""CUSTOM   SUPPLI"", type text}, {""NAME"", type text}, {""ARRIVAL""" & _
        ", type datetime}, {""DEPARTURE"", type datetime}, {""EXP TYPE"", type text}, {""EXPLANATION"", type text}, {""DEBIT"", type number}, {""CREDIT"", type number}, {""PAYMENT DATE"", type datetime}, {""INVOICE DATE"", type datetime}, {""GUEST NAME"", type text}, {""SALES PERSON"", type any}, {""DAY"", type any}, {""MONTH"", type text}, {""YEAR"", Int64.Type}, {""NOTE""" & _
        ", type any}})," & vbCrLf & "    #""Filtered Rows"" = Table.SelectRows(#""Changed Type"", each ([NAME] = ""MURAD TRAVEL""))," & vbCrLf & "    #""Removed Columns"" = Table.RemoveColumns(#""Filtered Rows"",{""DATE"", ""ORDER"", ""ORDER TYPE"", ""CUSTOM   SUPPLI"", ""INVOICE DATE"", ""GUEST NAME"", ""SALES PERSON"", ""DAY""})" & vbCrLf & "in" & vbCrLf & "    #""Removed Columns"""


'TEST'Dim wb As Workbook
'TEST'Set wb = Workbooks.Add
'TEST'ThisWorkbook.Queries.Copy Before:=wb.Queries
'TEST'wb.SaveAs "C:\temp\test1.xlsx"

'TEST'    ActiveWorkbook.Queries.Copy
'TEST'    ActiveWorkbook.Queries.SaveAs Workbook.Path & "\" & Workbook.Name & ".xls"  
'TEST'     Set NewBook = Workbooks.Add
'TEST'  Workbooks("Whatever.xlsx").Worksheets("output").Range("A1:K10").Copy
'TEST'  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
'TEST'  NewBook.SaveAs Filename:=NewBook.Worksheets("Sheet1").Range("E3").Value
'TEST'    Workbooks.Add Workbook.Path & "\" & Workbook.Name & ".xls", FileFormat:=xlOpenXMLWorkbookMacroEnabled

'CREATE NEW SHEET & ADD PQ TABLE    
    Sheets.Add After:=ActiveSheet
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""JOURNAL (5)""" _
        , Destination:=Range("$A$3")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [JOURNAL (5)]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = False
        .ListObject.DisplayName = "JOURNAL__5"
        .Refresh BackgroundQuery:=False
    End With
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub

0 个答案:

没有答案