Create xml for each row for excel file

JAVEDR

Board Regular
Joined
Sep 17, 2019
Messages
79
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
Respected sir / mam, i am looking for automation or macro to generate xml for each row please find attached excel file where date is paste and seperate .xml file which need to be generated

seperate xml file & excel file path

thanking you for valuable time & feedback.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Something like this...
VBA Code:
Sub ExportToXML()

    Dim Filename As Range
    Dim FSO As Object
    Dim XML As Object
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    With ActiveSheet
        For Each Filename In .Range("B2:B" & GetLastRow("Sheet1"))
            Set XML = FSO.CreateTextFile( _
                        Filename:=ThisWorkbook.Path & "\" & Filename.Value & ".xml", _
                        Overwrite:=True)
           
            With Filename
                XML.WriteLine ("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>")
                XML.WriteLine ("    <File>")
                XML.WriteLine ("        <Date>" & .Offset(0, -1).Value & "</Date>")
                XML.WriteLine ("        <FileName>" & .Value & "</FileName>")
                XML.WriteLine ("        <FileExtension>" & .Offset(0, 1).Value & "</FileExtension>")
                XML.WriteLine ("        <Title>" & .Offset(0, 2).Value & "</Title>")
                XML.WriteLine ("        <Mappings>")
                XML.WriteLine ("            <Mapping>")
                XML.WriteLine ("                <RICCode>" & .Offset(0, 3).Value & "</RICCode>")
                XML.WriteLine ("                <SEDOL>" & .Offset(0, 4).Value & "</SEDOL>")
                XML.WriteLine ("                <ISIN>" & .Offset(0, 5).Value & "</ISIN>")
                XML.WriteLine ("                <BBGTicker>" & .Offset(0, 6).Value & "</BBGTicker>")
                XML.WriteLine ("            </Mapping>")
                XML.WriteLine ("        </Mappings>")
                XML.WriteLine ("    </File>")
            End With
           
            XML.Close
        Next Filename
    End With
   
    Set XML = Nothing
    Set FSO = Nothing

End Sub

Function GetLastRow(wkSheet As String) As Long

    With Worksheets(wkSheet)
        GetLastRow = .Cells.Find( _
                        What:="*", _
                        LookIn:=xlValues, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious).Row
    End With

End Function
 
Upvote 0
million thanks sir this is what i was looking for :):):)
 
Upvote 0
Just one query when i run macro & xml files are exported in this line DATA is changed "<Date>7/12/2018 5:02:13 PM</Date>"

as i need data to be displayed "<Date>2018-07-12T17:02:13Z</Date>" Like this.
 
Upvote 0
Change .Value to .Text
VBA Code:
XML.WriteLine ("        <Date>" & .Offset(0, -1).Text & "</Date>")

I have tried above but still data is exported "<Date>2018-07-12T17:2:13Z</Date>" instead of "<Date>2018-07-12T17:02:13Z</Date>" Like this.

i.e -- 17:2 where as data need to be shown 17:02
 
Upvote 0
In the code declare this line
VBA Code:
Dim v, x, i As Integer, s As String

and implement those lines
VBA Code:
                xml.WriteLine ("    <File>")
                
                
                v = Split(.Offset(, -1).Text, "T")
                x = Split(v(1), ":")
                s = Empty
                For i = LBound(x) To UBound(x)
                    s = s & IIf(s > Empty, ":", Empty) & Format(Replace(x(i), "Z", ""), "00")
                Next i

                xml.WriteLine ("        <Date>" & v(0) & "T" & s & "Z" & "</Date>")
 
Upvote 0
In the code declare this line
VBA Code:
Dim v, x, i As Integer, s As String

and implement those lines
VBA Code:
                xml.WriteLine ("    <File>")
               
               
                v = Split(.Offset(, -1).Text, "T")
                x = Split(v(1), ":")
                s = Empty
                For i = LBound(x) To UBound(x)
                    s = s & IIf(s > Empty, ":", Empty) & Format(Replace(x(i), "Z", ""), "00")
                Next i

                xml.WriteLine ("        <Date>" & v(0) & "T" & s & "Z" & "</Date>")


Hello sir,

where to implement & declare this codes can you please advise.
 
Upvote 0
After those lines
VBA Code:
  Dim Filename As Range
    Dim FSO As Object
    Dim XML As Object

and you can declare them just before the new lines (no matter)
 
Upvote 0
After those lines
VBA Code:
  Dim Filename As Range
    Dim FSO As Object
    Dim XML As Object

and you can declare them just before the new lines (no matter)
ERROR.jpg


hello sir, i'm getting attached error.
 

Attachments

  • ERROR.jpg
    ERROR.jpg
    90.7 KB · Views: 44
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top