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>