1

I am trying to write to a excel more 50000 rows that etch row have 11 cells it's taking me more them 18 minutes to do so. can somebody tell me what am I am doing wrong?? I am seeing that most of the time is spending on writing into the Values Variant and not the actual writing into the excel

Thanks Itay

 public Sub updateResultsSheet()
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim NewBook As Excel.Workbook: Set NewBook = ActiveWorkbook
    Dim suppDistBranchId As String
    Dim suppProdId As String
    Dim reportingDate As String
    Dim query As String
    Dim nodeCell As IXMLDOMNode
    Dim rowCount As Integer
    Dim cellCount As Integer
    Dim rowRange As Range
    Dim cellRange As Range
    rowCount = 1
    query = "http://******:8080/RS_Excel_API/dailyInvHist/get/1?"
    reportingDate = Trim(Range("Parameters!F" + CStr(2)).Value & vbNullString)
     query = query + "reportingDate="
     query = query + reportingDate
    Dim Req As New XMLHTTP
    Req.Open "GET", query, False
    Req.send
    Dim Resp As New DOMDocument
    Resp.LoadXML Req.responseText
    Dim InventoyHistory As IXMLDOMNode

    Application.Visible = True
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
    ws.DisplayPageBreaks = False



    Dim OrigCalc As Excel.XlCalculation
    OrigCalc = Application.Calculation
    Application.Calculation = xlCalculationManual

    Const BlockSize As Long = 1000
    Dim Values() As Variant
    ReDim Values(BlockSize, 11)
    Dim idx As Long
    idx = 1
    Dim RowNumber As Long
    RowNumber = 2
    Dim celInx As Integer
    Resp.getElementsByTagName ("DailyInventoryHistory")
    celInx = 0
     Dim StartTime As Double
      StartTime = Timer
     For Each InventoyHistory In Resp.getElementsByTagName("DailyInventoryHistory")
    celInx = 0
        For Each nodeCell In InventoyHistory.ChildNodes
            Values(idx, celInx) = nodeCell.nodeTypedValue
            celInx = celInx + 1
        Next nodeCell
        idx = idx + 1
        If idx = BlockSize - 1 Then
            With ws
                .Range(.Cells(RowNumber, 1), .Cells(RowNumber + BlockSize - 1, 11)).Value = Values
            End With
            idx = 1
            ReDim Values(BlockSize, 11)
            RowNumber = RowNumber + BlockSize
        End If
    Next
    ' write last block
    With ws
        .Range(.Cells(RowNumber, 1), .Cells(RowNumber + BlockSize - 1, 11)).Value = Values
    End With
    Application.ScreenUpdating = True
    Application.Calculation = OrigCalc
    Application.Visible = True

    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    ws.DisplayPageBreaks = True
     MsgBox Format(Timer - StartTime, "0000.00") & " seconds"

    End Sub
XMl exmple:
   <xML_DailyInventoryHistories>
    <DailyInventoryHistory>
    <calcOp>0</calcOp>
    <calcOq>1</calcOq>
    <dmiDistBranchId>0</dmiDistBranchId>
    <netQtyAvailable>0</netQtyAvailable>
    <qtyAvailable>0</qtyAvailable>
    <qtyCommittedToSale>0</qtyCommittedToSale>
    <qtyOnHand>0</qtyOnHand>
    <qtySold>0</qtySold>
    <supplierNetPrice>0.599</supplierNetPrice>
    <usedOp>0</usedOp>
    <usedOq>1</usedOq>
    </DailyInventoryHistory>
    <DailyInventoryHistory>
    <calcOp>0</calcOp>
    <calcOq>1</calcOq>
    <dmiDistBranchId>0</dmiDistBranchId>
    <netQtyAvailable>0</netQtyAvailable>
    <qtyAvailable>0</qtyAvailable>
    <qtyCommittedToSale>0</qtyCommittedToSale>
    <qtyOnHand>0</qtyOnHand>
    <qtySold>0</qtySold>
    <supplierNetPrice>0.599</supplierNetPrice>
    <usedOp>0</usedOp>
    <usedOq>1</usedOq>
    </DailyInventoryHistory>
</xML_DailyInventoryHistories>
itaya
  • 11
  • 2
  • Please post a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Code alone does not help us re-create your issue. Plus, the actual XML sample and desired results of what you are intending to do might help. – Parfait Feb 05 '16 at 21:18
  • Any possibility of saving the file down and using "OpenXML"? – Kyle Feb 05 '16 at 21:33
  • unfortunately cannot use OpenXML.... – itaya Feb 08 '16 at 17:45
  • I added an XML exmple to the code – itaya Feb 08 '16 at 18:03
  • [This answer](http://stackoverflow.com/a/365372/2127508) on transforming an XML file to CSV using XSLT might be useful. You could then just open the CSV directly in Excel – barrowc Feb 08 '16 at 18:51
  • I don't think that this answer helps in my case.. I have an excel sheet that in this sheet I have a macro that send a WS request server and get back XML. then I am trying to write the objects over there to the same excel of the macro. my problem is that the loop that I am doing inside the macro code in order to insert the rows into the VARIANT ("Values()" see code above) is taking to much time( something like 20 seconds for a 1000 rows) – itaya Feb 08 '16 at 19:14
  • Anybody can help me with this? I am still stuck – itaya Feb 11 '16 at 13:46

0 Answers0