我有一些通过 VBA 加载 XML 文件的 VBA。但是,当它被导入时,它都在一个列中,而不是拆分成一个表。
当我通过“数据”选项卡手动导入时,我收到警告,没有架构,但询问我是否希望 Excel 根据源数据创建一个架构。然后将所有数据放在一个漂亮的表中。
我希望这在我当前的 VBA 代码中自动发生:
VBA 看起来像
Sub refresh()
'--------------------------------1. Profile IDs-----------------------------------'
'date variables
Dim start_period As String
start_period = Sheets("Automated").Cells(1, 6).Value
Dim end_period As String
end_period = Sheets("Automated").Cells(1, 7).Value
'report id variable names
Dim BusinessplanningReportID As String
'--------------------------------REST queries--------------------------------'
Dim Businessplanning As String
'REST query values
Businessplanning = "URL;http://api.trucast.net/2/saved_searches/00000/pivot/content_volume_trend/?apikey=0000000&start=" + start_period + "&end=" + end_period + "&format=xml"
'--------------------------------------------Data connections-----------------------------------'
'key metrics
With Worksheets("Sheet1").QueryTables.Add(Connection:=Businessplanning, Destination:=Worksheets("Sheet1").Range("A1"))
.RefreshStyle = xlOverwriteCells
.SaveData = True
End With
目前,数据然后以这样的方式呈现自己,非结构化。我怎样才能自动把它变成一张桌子?
<result>
<entry>
<published_date>20130201</published_date>
<post_count>18</post_count>
</entry>
谢谢,
::最终解决方案::
Sub XMLfromPPTExample2()
Dim XDoc As MSXML2.DOMDocument
Dim xresult As MSXML2.IXMLDOMNode
Dim xentry As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim start_period As String
start_period = Sheets("Automated").Cells(1, 6).Value
Dim end_period As String
end_period = Sheets("Automated").Cells(1, 7).Value
Dim wb As Workbook
Dim Col As Integer
Dim Row As Integer
Set XDoc = New MSXML2.DOMDocument
XDoc.async = False
XDoc.validateOnParse = False
XDoc.Load ("http://api.trucast.net/2/saved_searches/0000/pivot/content_volume_trend/?apikey=00000&start=" + start_period + "&end=" + end_period + "&format=xml")
LoadOption = xlXmlLoadImportToList
Set xresult = XDoc.DocumentElement
Set xentry = xresult.FirstChild
Col = 1
Row = 1
For Each xentry In xresult.ChildNodes
Row = 1
For Each xChild In xentry.ChildNodes
Worksheets("Sheet2").Cells(Col, Row).Value = xChild.Text
'MsgBox xChild.BaseName & " " & xChild.Text
Row = Row + 1
'Col = Col + 1
Next xChild
'Row = Row + 1
Col = Col + 1
Next xentry
End Sub