Search code examples
excelvba

Importing XML to Excel error data imported as text


I'm trying to import XML files to Excel.

It returns an error

some data was imported as text

I have to press okay for the code to continue.

I tried to insert error handlers.

Sub Import()
    Dim XMLFileName As String
    Dim SavePath As String
    Dim WB As Workbook

    XMLFileName = "D:\Reservation Statistics.xml"
    
    SavePath = "D:\Reservation Statistics.xlsx"
    
    Application.ScreenUpdating = False
    
    Set WB = Workbooks.Add
    
    WB.XmlImport Url:=XMLFileName, ImportMap:=Nothing, Overwrite:=True, Destination:=WB.Sheets(1).Range("A1")
    
    WB.SaveAs SavePath, FileFormat:=xlOpenXMLWorkbook
        
    WB.Close SaveChanges:=False
    
    Application.ScreenUpdating = True
    
End Sub

This is the error
enter image description here

For fixing the number stored as a text later I use this code.

Sub txttonum()

    Dim rngRange As Range
    Set rngRange = ActiveSheet.Range("A1:A20")
    rngRange.Value = rngRange.Value * 1
End Sub

Solution

  • i could ignore/hid the error by using Application.DisplayAlerts = False so the final code is

    Sub Import1()
    Dim XMLFileName As String
    Dim SavePath As String
    Dim WB As Workbook
    
    XMLFileName = "D:\Reservation Statistics.xml"
    
    SavePath = "D:\Reservation Statistics.xlsx"
    
    Application.ScreenUpdating = False
    
    Application.DisplayAlerts = False
    
    Set WB = Workbooks.Add
    
    WB.XmlImport Url:=XMLFileName, ImportMap:=Nothing, Overwrite:=True, Destination:=WB.Sheets(1).Range("A1")
    
    WB.SaveAs SavePath, FileFormat:=xlOpenXMLWorkbook
        
    WB.Close SaveChanges:=False
    
    Application.DisplayAlerts = True
    
    Application.ScreenUpdating = True
    

    End Sub