HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Copying orders and prices into another Excel sheet

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
excelintosheetpricescopyinganotherandorders

Problem

I would like to know if the below VBA can be streamlined to process faster, since it takes on average 9hrs to complete a sheet (800 000 lines), and I have quite a few to get through. Running on 3 computers already to get ahead of the work.

```
Sub Calculate_Sheet()

Dim orderSh As Worksheet
Dim wiroSh As Worksheet
Dim lastRow As Long, r As Long
Dim pctComp As Double
Dim Arr1 As Variant
Dim StartTime As Double
Dim MinutesElapsed As String

StartTime = Timer

With ThisWorkbook
'calculator
Set orderSh = .Sheets("ORDER")
'price list
Set wiroSh = .Sheets("WiroA3C100gsmI100gsm116-118pp ")
End With

Arr1 = wiroSh.Range("C1:M800001")
lastRow = wiroSh.Cells(Rows.Count, 3).End(xlUp).Row

For r = 2 To 2

'copy from price list to calculator
orderSh.Range("F4") = Arr1(r, 1)
orderSh.Range("F5") = Arr1(r, 2)
orderSh.Range("F6") = Arr1(r, 3)
orderSh.Range("F7") = Arr1(r, 4)
orderSh.Range("F8") = Arr1(r, 5)
orderSh.Range("F9") = Arr1(r, 6)
orderSh.Range("F10") = Arr1(r, 7)
orderSh.Range("F11") = Arr1(r, 8)
orderSh.Range("F12") = Arr1(r, 9)
orderSh.Range("F13") = Arr1(r, 10)

'copy result
wiroSh.Range("m" & r).Value = orderSh.Range("F14")

Next r

For r = 3 To 400001

'copy from price list to calculator
orderSh.Range("F4") = Arr1(r, 1)

'copy result
wiroSh.Range("m" & r).Value = orderSh.Range("F14")

'display the row and percentage each 1000 rows
If r Mod 1 = 0 Then
Application.StatusBar = "Row = " & r & Format(r / lastRow, " #0.00%")
End If

Next r

For r = 400002 To 400002
'copy from price list to calculator
orderSh.Range("F4") = Arr1(r, 1)
orderSh.Range("F5") = Arr1(r, 2)
orderSh.Range("F6") = Arr1(r, 3)
orderSh.Range("F7") = Arr1(r, 4)
orderSh.Range("F8") = Arr1(r, 5)
orderSh.Range("F9") = Arr1(r, 6)
orderSh.Range("F10") = Arr1(r, 7)
orderSh.Range("F11") = A

Solution

There's likely many factors at play here, but here's my two cents:

It looks like you're copying whole cell's onto another sheet for calculation, and again to a results sheet.

What will be happening here is that when copying the values, it will be copying all the information about cells and not just the values. By adding a cell to the watch list you can see just how much information is there that in most cases you don't need (this is just a small section of the entire list):

I would suggest reviewing your copy method to copy only the information you actually need (quite possibly just .Value), this will save both memory and processing.

Context

StackExchange Code Review Q#128466, answer score: 3

Revisions (0)

No revisions yet.