Grasor
Board Regular
- Joined
- May 16, 2014
- Messages
- 123
- Office Version
- 365
- Platform
- Windows
Hi all,
For practice I like to program overbuilt spreadsheets for useless causes. Below is a part of one of them.
Despite turning off screen updating and calculations this still runs extremely slow.
So far this code is only having to handle about 600 "rows" of data; and frankly it's not that much data - only 6 entries per line. Yet it takes about a full 1min30s to build the array and post the result. That's just slow; particularly because I plan on expanding the amount of data to interpret to thousands of rows.
There has to be a faster way. Any ideas?
P.S. +1 to anyone who can name the application that I'm dealing with.
For practice I like to program overbuilt spreadsheets for useless causes. Below is a part of one of them.
Despite turning off screen updating and calculations this still runs extremely slow.
So far this code is only having to handle about 600 "rows" of data; and frankly it's not that much data - only 6 entries per line. Yet it takes about a full 1min30s to build the array and post the result. That's just slow; particularly because I plan on expanding the amount of data to interpret to thousands of rows.
There has to be a faster way. Any ideas?
P.S. +1 to anyone who can name the application that I'm dealing with.
Code:
Type AssetsArray
TypeID As String
ItemName As String
LocationID As String
Container As String
Quantity As Long
Value As Variant
End Type
Sub CalculateAssets()
'This sub calculates assets list on demand. It supercedes in-sheet coding which has proven heavy on processing.
'Future upgrade notes: Function to return Station name instead of location ID.
Dim rXMLMap As Range
Dim lRow, i As Long
Dim AssetsXML() As AssetsArray
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Debug.Print Time
Set rXMLMap = Range(Range("I23"), Range("I100000").End(xlUp))
ReDim AssetsXML(0 To rXMLMap.Rows.Count)
lRow = 23
'Build Array of Pertinent Values from XML Map
For i = 0 To UBound(AssetsXML)
bIsContents = IIf(Range("U" & lRow) = "contents", True, False)
With AssetsXML(i)
.TypeID = IIf(bIsContents = True, MyAssets.Range("Y" & lRow), MyAssets.Range("P" & lRow))
.ItemName = FindMktID(.TypeID, "ItemName")
.LocationID = MyAssets.Range("O" & lRow)
.Container = IIf(bIsContents = False, "Station", FindMktID(MyAssets.Range("P" & lRow), "ItemName"))
.Quantity = MyAssets.Range("Q" & lRow)
.Value = FindMktID(.TypeID, "MktValue")
End With
lRow = lRow + 1
Next i
'Reset lRow to first row in table
lRow = 23
'Post Array to Spreadsheet
For i = 0 To UBound(AssetsXML)
With AssetsXML(i)
Range("A" & lRow) = .TypeID
Range("B" & lRow) = .ItemName
Range("C" & lRow) = .LocationID
Range("D" & lRow) = .Container
Range("E" & lRow) = .Quantity
Range("F" & lRow) = .Value
End With
lRow = lRow + 1
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Debug.Print Time
End Sub
Function FindMktID(TypeID As String, FindWhat As String) As String
Dim rCell, rMarketIDs As Range
Dim btOffset As Byte
btOffset = IIf(FindWhat = "ItemName", 1, IIf(FindWhat = "MktValue", 2, 1)) 'If FindWhat Arg is "ItemName" offset 1, If "MktValue" offset 2.
Set rMarketIDs = MktValue.Range(MktValue.Range("A1"), MktValue.Range("A100000").End(xlUp))
For Each rCell In rMarketIDs
On Error GoTo NextCell
If rCell.Value = TypeID Then
FindMktID = rCell.Offset(0, btOffset)
End If
NextCell:
Next rCell
End Function
Last edited: