10

I used the following code to fetch data from XML to Excel. But the problem is, the results are opened in a new workbook "Book1". But I want to get the results in a specific worksheet in the same Excel where I have this macro.
Please note that I don't want to create schema in the code, as the schema changes for all the XMLs. Below mentioned code does not need schema to be specified. It can dump the results in new Excel sheet with the proper column names. So, Please let me know how can I get the results in the sheet2 in the same workbook?

Sub ImportXMLtoList()
 Dim strTargetFile As String
 Application.DisplayAlerts = False
 strTargetFile = "C:\example.xml"
 Workbooks.OpenXML Filename:=strTargetFile, LoadOption:=xlXmlLoadImportToList
 Application.DisplayAlerts = True

End Sub

2 Answers 2

20
Sub ImportXMLtoList()
Dim strTargetFile As String
Dim wb as Workbook

     Application.Screenupdating = False
     Application.DisplayAlerts = False
     strTargetFile = "C:\example.xml"
     Set wb = Workbooks.OpenXML(Filename:=strTargetFile, LoadOption:=xlXmlLoadImportToList)
     Application.DisplayAlerts = True

     wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet2").Range("A1")
     wb.Close False
     Application.Screenupdating = True


End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

hey Tim, thanks a lot......... this worked :). It was my bad, I didnt specify the sheet name properly.
Thanks this was a huge help. But for me xlXmlLoadImportToList caused an error. I tried xlXmlLoadPromptUser and found that the read-only option worked, which I found is the LoadOption:=xlXmlLoadOpenXml. And that works. Thanks again.
0

i created a simply xmlmap. I need a code to import xml data to excel ( right click-xml-import), to get data from an xml, or another. I want to keep the xmlmap that I created, and just replace the data in my xlxs to data from another xlm.

'Select the file
Fname = Application.GetOpenFilename(FileFilter:="xml files (*.xml), *.xml", MultiSelect:=False)

'Check if file selected
If Fname = False Then
    Exit Sub
    Else
Workbooks.Open Filename:=Fname
End If

1 Comment

V Good ... still works good even in 2021

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.