使用VB脚本更改Power Query的数据源

时间:2019-09-18 19:24:52

标签: excel vba powerquery

以下第vba代码在第4行替换为

时有效

...源= Json.Document(Web.Contents(“” https://pidynaapst02.com/e/78df7c69-3512-4946-8d3c-f1c0e451fe80/api/v1/problem/feed?relativeTime=month“”)),“&Chr(13)&...

但是,为了自动执行此操作,我想用一个变量替换url,如下所示:

...源= Json.Document(Web.Contents(“”“&VA&”“”)),“&Chr(13)&...

这是完整的vb代码:

`                                                                                                 
Sub Macro2()                                                                                      
VA = Sheets(1).Range("A1").Value
ActiveWorkbook.Queries.Add Name:= _
    "Table 0", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Json.Document(Web.Contents(""" & VA & """)," & Chr(13) & "" & Chr(10) & "    result = Source[result]," & Chr(13) & "" & Chr(10) & "    problems = result[problems]," & Chr(13) & "" & Chr(10) & "    #""Converted to Table"" = Table.FromList(problems, Splitter.SplitByNothing(), null, null, ExtraValues." & _
    "Error)," & Chr(13) & "" & Chr(10) & "    #""Expanded Column1"" = Table.ExpandRecordColumn(#""Converted to Table"", ""Column1"", {""id"", ""startTime"", ""endTime"", ""displayName"", ""impactLevel"", ""status"", ""severityLevel"", ""commentCount"", ""tagsOfAffectedEntities"", ""rankedImpacts"", ""affectedCounts"", ""recoveredCounts"", ""hasRootCause""}, {""Column1.id"", ""Column1.startTime"", " & _
    """Column1.endTime"", ""Column1.displayName"", ""Column1.impactLevel"", ""Column1.status"", ""Column1.severityLevel"", ""Column1.commentCount"", ""Column1.tagsOfAffectedEntities"", ""Column1.rankedImpacts"", ""Column1.affectedCounts"", ""Column1.recoveredCounts"", ""Column1.hasRootCause""})," & Chr(13) & "" & Chr(10) & "    #""Expanded Column1.tagsOfAffectedEntities"" = Table.ExpandListColumn(#" & _
    """Expanded Column1"", ""Column1.tagsOfAffectedEntities"")," & Chr(13) & "" & Chr(10) & "    #""Expanded Column1.tagsOfAffectedEntities1"" = Table.ExpandRecordColumn(#""Expanded Column1.tagsOfAffectedEntities"", ""Column1.tagsOfAffectedEntities"", {""context"", ""key"", ""value""}, {""Column1.tagsOfAffectedEntities.context"", ""Column1.tagsOfAffectedEntities.key"", ""Column1.tagsOfAffectedEntit" & _
    "ies.value""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Expanded Column1.tagsOfAffectedEntities1"""Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0""" _
    , "ended Properties="""""), Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array( _
    "SELECT * FROM [Table 0]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = False
    .ListObject.DisplayName = "Table_0"
    .Refresh BackgroundQuery:=False
End WithSelection.ListObject.QueryTable.Refresh BackgroundQuery:=False                            
End Sub`

出现错误“数据源初始化失败。请检查数据库服务器或与您的数据库管理员联系。请确保外部数据库可用,然后再次尝试操作。如果再次看到此消息,请创建一个新的数据源以连接到数据库。”

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

我认为您在M代码的第一行中缺少结尾)。高级编辑器显示错误:

Error message

我无法测试是否可以解决您的问题。但是,我建议您不要在VBA中操纵查询的公式(因为您不能确定结果在M上的语法上是否有效)。

根据您的VBA代码,您的查询与用于使查询公式“动态”的范围/单元格在同一工作簿中。如果您将这些范围/单元格(例如,单元格A1)转换为命名范围,则可以在Power Query中使用Excel.CurrentWorkbook来读取那些命名范围中的值。

这意味着您无需将这些单元格的值推送到Power Query。相反,Power Query可以自行读取它们-您的查询仍然是“动态的”。


次要细节。您的M代码包含Table.FromList,后跟Table.ExpandRecordColumn。这两个步骤都有可能被Table.FromRecords取代。