【问题标题】:Issue with Power Query absolute path in VBAVBA 中的 Power Query 绝对路径问题
【发布时间】:2021-12-17 13:58:06
【问题描述】:

我正在尝试编写代码提示用户选择要用于 Power Query 的文件,但我收到错误消息“提供的路径必须是有效的绝对路径”。有没有办法修复它,所以路径是动态的?

这是我的代码:

Sub Insert_test()


    FileToOpen = Application.GetOpenFilename()

 
    ActiveWorkbook.Queries.Add Name:="Stock On Hand Report", Formula:= _

        "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""&FileToOpen&""), null, true)," & Chr(13) & "" & Chr(10) & "    #""Stock On Hand Report1"" = Source{[Name=""Stock On Hand Report""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Stock On Hand Report1"",{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type" & _

        " text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", " & _

        "type text}, {""Column18"", type text}})," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"" = Table.SelectColumns(#""Changed Type"",{""Column3"", ""Column4"", ""Column7"", ""Column13""})," & Chr(13) & "" & Chr(10) & "    #""Filtered Rows"" = Table.SelectRows(#""Removed Other Columns"", each [Column7] = ""207"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Filtered Rows"""

    ActiveWorkbook.Worksheets.Add

    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _

        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Stock On Hand Report"";Extended Properties=""""" _

        , Destination:=Range("$A$1")).QueryTable

        .CommandType = xlCmdSql

        .CommandText = Array("SELECT * FROM [Stock On Hand Report]")

        .RowNumbers = False

        .FillAdjacentFormulas = False

        .PreserveFormatting = True

        .RefreshOnFileOpen = False

        .BackgroundQuery = True

        .RefreshStyle = xlInsertDeleteCells

        .SavePassword = False

        .SaveData = True

        .AdjustColumnWidth = True

        .RefreshPeriod = 0

        .PreserveColumnInfo = True

        .ListObject.DisplayName = "Stock_On_Hand_Report"

        .Refresh BackgroundQuery:=False

    End With

End Sub

【问题讨论】:

    标签: vba powerquery


    【解决方案1】:

    随便用

    File.Contents(" & Chr(34) & FileToOpen & Chr(34) & ")
    

    整行应该是这样的

    ActiveWorkbook.Queries.Add Name:="Stock On Hand Report", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(" & Chr(34) & FileToOpen & Chr(34) & "), null, true)," & Chr(13) & "" & Chr(10) & "    #""Stock On Hand Report1"" = Source{[Name=""Stock On Hand Report""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Stock On Hand Report1"",{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type" & _
        " text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", " & _
        "type text}, {""Column18"", type text}})," & Chr(13) & "" & Chr(10) & "    #""Removed Other Columns"" = Table.SelectColumns(#""Changed Type"",{""Column3"", ""Column4"", ""Column7"", ""Column13""})," & Chr(13) & "" & Chr(10) & "    #""Filtered Rows"" = Table.SelectRows(#""Removed Other Columns"", each [Column7] = ""207"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Filtered Rows"""
    

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 2020-07-04
      • 2016-07-24
      相关资源
      最近更新 更多