Hello fellow stackholders
I have this in-efficient VBA macro where i convert rows to XMl and after that post it to a web-service. It all works fine and it post everything correctly - the problem is when the excel sheet has more than 1500 rows, then it takes forever to convert. it takes hours, if you go above 10 k lines (had a co-worker who tried).
My question: Is there a way for me to speed this up, so 10.000 rows wont take half a day?
So far my code looks like this:
Dim xDOC As DOMDocument
Dim XMLHttpRequest As XMLHTTP
Dim URL As String
' Variables for XML-bulk
Dim strXML As String
Dim varTable As Variant
Dim intRow As Integer
Dim intCol As Integer
Dim strRowElementName As String
Dim strTableElementName As String
Dim varColumnHeaders As Variant
' Set custom names
strTableElementName = "postdata"
strRowElementName = "general-journal-line"
' Select the whole table in the current sheet
Range("A7").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
' Get table data
varTable = Selection.Value
varColumnHeaders = Selection.Rows(1).Value
' Build XML
strXML = ""
strXML = strXML & "<" & strTableElementName & ">"
For intRow = 2 To UBound(varTable, 1)
strXML = strXML & "<" & strRowElementName & ">"
strXML = strXML & "<journal-template-name>KASSE</journal-template-name>"
strXML = strXML & "<journal-batch-name>" + ThisWorkbook.Sheets("SMARTapi-Opsaetning").Range("C8") + "</journal-batch-name>"
strXML = strXML & "<userid>" + Environ("computername") + "\" + Application.UserName + "</userid>"
strXML = strXML & "<account-type>G/L Account</account-type>"
For intCol = 1 To UBound(varTable, 2)
strXML = strXML & "<" & varColumnHeaders(1, intCol) & ">" & _
varTable(intRow, intCol) & "</" & varColumnHeaders(1, intCol) & ">"
Next
strXML = strXML & "</" & strRowElementName & ">"
Next
strXML = strXML & "</" & strTableElementName & ">"
Debug.Print strXML
After this i post it at a webservice:
Set XMLHttpRequest = New MSXML2.XMLHTTP
With XMLHttpRequest
.Open "POST", URL, False
.setRequestHeader "Content-Type", "text/xml; encoding='utf-8'"
.Send strXML
End With
Set xDOC = New DOMDocument
Do Until xDOC.readyState = 4
Loop
xDOC.LoadXML (XMLHttpRequest.responseText)
It all works great when there is less than 500 rows - any help to make it more efficient would be much appreciated.
EDIT: Changed the code to this, yet it is still somewhat slow.
Dim xDOC As DOMDocument
Dim XMLHttpRequest As XMLHTTP
Dim URL As String
' Variabler til XML-bulk
Dim strXML As String
Dim varTable As Variant
Dim intRow As Integer
Dim intCol As Integer
Dim strRowElementName As String
Dim strTableElementName As String
Dim varColumnHeaders As Variant
Dim strKonstant As String
' Set custom names
strTableElementName = "postdata"
strRowElementName = "general-journal-line"
' Find lines and get them before building the xml
Dim lRowCount As Long
Application.ActiveSheet.UsedRange
lRowCount = Worksheets("SMARTapi-Upload").UsedRange.Rows.Count
varTable = Range("A7", "J" + CStr(lRowCount))
varColumnHeaders = Range("A7", "J7")
strKonstant = "<" & strRowElementName & "><journal-template-name>KASSE</journal-template-name><journal-batch-name>" + ThisWorkbook.Sheets("SMARTapi-Opsaetning").Range("C8") + "</journal-batch-name><userid>" + Environ("computername") + "\" + Application.UserName + "</userid><account-type>G/L Account</account-type><balancing-account-type>G/L Account</balancing-account-type>"
' Build XML
strXML = ""
strXML = strXML & "<" & strTableElementName & ">"
For intRow = 2 To UBound(varTable, 1)
strXML = strXML & strKonstant
For intCol = 1 To UBound(varTable, 2)
strXML = strXML & "<" & varColumnHeaders(1, intCol) & ">" & _
varTable(intRow, intCol) & "</" & varColumnHeaders(1, intCol) & ">"
Next
strXML = strXML & "</" & strRowElementName & ">"
Next
strXML = strXML & "</" & strTableElementName & ">"
' HER SENDES XML MED DATA FRA TABELLEN
Set XMLHttpRequest = New MSXML2.XMLHTTP
With XMLHttpRequest
.Open "POST", URL, False
.setRequestHeader "Content-Type", "text/xml; encoding='utf-8'"
.Send strXML
End With
Set xDOC = New DOMDocument
Do Until xDOC.readyState = 4
Loop
xDOC.LoadXML (XMLHttpRequest.responseText)