VBA 中的 Power Query 绝对路径问题

Posted

技术标签:

【中文标题】VBA 中的 Power Query 绝对路径问题【英文标题】:Issue with Power Query absolute path in VBA 【发布时间】: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

【问题讨论】:

【参考方案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"""

【讨论】:

以上是关于VBA 中的 Power Query 绝对路径问题的主要内容,如果未能解决你的问题,请参考以下文章

如何像 Power Query 一样使用 VBA 取消固定数量的列

Power Query - 用零替换负数

【OFFICE 365】Power Query 多工作簿合并

Excel 2016 Power Query无法加载到电子表格

1.大道至简的数据处理工具(Power Query)

1.大道至简的数据处理工具(Power Query)