I've added a timer to my code and the bottleneck comes when I'm looping through 47 rows and inputting data from a dictionary that I previously loaded with values.
Since i use these files for a lot of different things I've set up public variables in order to avoid setting them up for every new code.
So my question is, is there a quicker way to pull data from the dictionary based on criteria in specific cells? The line directly below is repeated 8 times for the 8 different columns being populated with dictionary data, each column takes .20 seconds to complete, so 1.6 seconds per each iteration in the w loop (orderStart to orderEnd).
Cells(w, OF_clearanceColumn1) = stationclearanceData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_clearanceColumn1).Value)
Timer stats:
3.4453125 Open client database, determine which account we're on
3.484375 Find last 4 weks and order range
7.31640625 Adding columns and formating
7.61328125 loop through last 4 weeks, add station clearance and P&L data to dictionary
7.6484375 find range of cumulative, add clearance and P&L for 2019
100.90234375 adding data from dictionary to order file
NEW TIMER STATS, THANKS TO AJD AND MOVING DATA FROM DICTIONARY TO ARRAY TO RANGE.
1.71875 Open client database, determine which account we're on
1.75 Find last 4 weeks and order range
5.3203125 Adding columns and formating
5.6171875 loop through last 4 weeks, add station clearance and P&L data to dictionary
5.6640625 find range of cumulative, add clearance and P&L for 2019
7.6171875 adding data from dictionary to order file
Anyway, below is the code...
Sub Orders_Historicals_autofilterdict()
Dim start As Double
start = Timer
''--------------------------------------
''Static Variables
''--------------------------------------
Call DefinedVariables
Dim orderFile As Variant
Dim orderStart As Long
Dim orderEnd As Long
Dim clientdataFile As Variant
Dim internalFile As Variant
Dim dateStart As Long
Dim stationStart As Long
Dim stationEnd As Long
Dim currentStation As String
Dim currentWeek As String
Dim dictData As New Scripting.Dictionary
Dim stationclearanceData As New Scripting.Dictionary
Dim stationplData As New Scripting.Dictionary
Dim key As Variant
Dim fileOnly As String
Dim networkOnly As String
Dim i As Long
Dim w As Long
Dim t As Long
Dim plTotal As Long
Dim clearTotal As Long
Dim stationHash As String
''--------------------------------------
''Dictionary the Order Abbreviations
''--------------------------------------
Application.ScreenUpdating = False
Set orderFile = ActiveWorkbook.ActiveSheet
Workbooks.Open clientdataLocation
Set clientdataFile = ActiveWorkbook.Sheets(dan_location) '/ Change sheet when using on different computer
clientdataFile.Activate
For i = 1 To Cells(Rows.count, 1).End(xlUp).row
If dictData.Exists(Cells(i, clientOrder).Value) Then
Else: dictData.Add Cells(i, clientOrder).Value, i
End If
Next
''--------------------------------------
''Determine Account/Network & Open Internal Associated with Order
''--------------------------------------
orderFile.Activate
fileOnly = ActiveWorkbook.Name
fileOnly = Left(fileOnly, InStr(fileOnly, ".") - 1)
If InStr(fileOnly, 2) > 0 Or InStr(fileOnly, 3) > 0 Then
fileOnly = Left(fileOnly, Len(fileOnly) - 1)
End If
networkOnly = ActiveWorkbook.Name
networkOnly = Mid(networkOnly, InStr(networkOnly, "IO.") + 3)
networkOnly = Left(networkOnly, InStr(networkOnly, ".") - 1)
Workbooks.Open Filename:=clientdataFile.Cells(dictData(fileOnly), clientInternal).Value
Set internalFile = ActiveWorkbook
internalFile.Sheets(WT_newWeek).Activate
Debug.Print Timer - start & " Open client database, determine which account we're on"
''--------------------------------------
''Find Last 4 Dates & Column Header for Orders
''--------------------------------------
For i = 1 To 700
If Cells(i, 1) = WT_newWeek Then
dateStart = i
ElseIf Cells(i, 1) = "Station" Then
stationStart = i + 1
Exit For
End If
Next
For i = stationStart To 700
If Cells(i, 1).Value = Cells(stationStart - 2, 1).Value & " Total" Then
stationEnd = i - 1
Exit For
End If
Next
orderFile.Activate
For i = 1 To 700
If Cells(i, 1) = "Station" Then
orderStart = i + 1
Exit For
End If
Next
For i = orderStart To 700
If Len(Cells(i, 1)) = 0 And Len(Cells(i - 1, 1)) = 0 And Len(Cells(i - 2, 1)) = 0 Then
orderEnd = i - 3
Exit For
End If
Next
Debug.Print Timer - start & " Find last 4 weeks and order range"
''--------------------------------------
''Add Dates to Order Header and Formatting
''--------------------------------------
Cells(orderStart - 1, OF_buyAlgoColumn) = "Algorithm Recommendation"
Cells(orderStart - 1, OF_totalplColumn) = "Total P&L"
Cells(orderStart - 1, OF_totalclearanceColumn) = "Total Clearance %"
Cells(orderStart - 1, OF_clearanceColumn1) = internalFile.Sheets(WT_newWeek).Cells(dateStart, 1)
Cells(orderStart - 1, OF_clearanceColumn2) = internalFile.Sheets(WT_newWeek).Cells(dateStart - 1, 1)
Cells(orderStart - 1, OF_clearanceColumn3) = internalFile.Sheets(WT_newWeek).Cells(dateStart - 2, 1)
Cells(orderStart - 1, OF_clearanceColumn4) = internalFile.Sheets(WT_newWeek).Cells(dateStart - 3, 1)
Cells(orderStart - 1, OF_plColumn1) = internalFile.Sheets(WT_newWeek).Cells(dateStart, 1)
Cells(orderStart - 1, OF_plColumn2) = internalFile.Sheets(WT_newWeek).Cells(dateStart - 1, 1)
Cells(orderStart - 1, OF_plColumn3) = internalFile.Sheets(WT_newWeek).Cells(dateStart - 2, 1)
Cells(orderStart - 1, OF_plColumn4) = internalFile.Sheets(WT_newWeek).Cells(dateStart - 3, 1)
Range(Cells(orderStart - 2, OF_clearanceColumn1), Cells(orderStart - 2, OF_clearanceColumn4)) = "Clearance"
Range(Cells(orderStart - 2, OF_plColumn1), Cells(orderStart - 2, OF_plColumn4)) = "P&L"
Cells(orderStart - 1, OF_stationColumn).Copy
Range(Cells(orderStart - 1, OF_buyAlgoColumn), Cells(orderStart - 1, OF_plColumn4)).PasteSpecial xlPasteFormats
Cells(orderStart, OF_stationColumn).Copy
Range(Cells(orderStart - 2, OF_clearanceColumn1), Cells(orderStart - 2, OF_plColumn4)).PasteSpecial xlPasteFormats
Range(Cells(orderStart - 2, OF_buyAlgoColumn), Cells(orderEnd, OF_plColumn4)).HorizontalAlignment = xlCenter
Cells(orderStart, OF_stationColumn).Copy
Range(Cells(orderStart, OF_buyAlgoColumn), Cells(orderEnd, OF_plColumn4)).PasteSpecial xlPasteFormats
Cells(orderStart, OF_totalColumn).Copy
Range(Cells(orderStart, OF_plColumn1), Cells(orderEnd, OF_plColumn4)).PasteSpecial xlPasteFormats
Range(Cells(orderStart, OF_totalplColumn), Cells(orderEnd, OF_totalplColumn)).PasteSpecial xlPasteFormats
Range(Cells(orderStart, OF_totalclearanceColumn), Cells(orderEnd, OF_clearanceColumn4)).NumberFormat = "0%"
Range(Cells(orderStart - 2, OF_buyAlgoColumn), Cells(orderEnd, OF_plColumn4)).FormatConditions.Delete
Range(Columns(OF_buyAlgoColumn), Columns(OF_plColumn4)).AutoFit
Debug.Print Timer - start & " Adding columns and formating"
''--------------------------------------
''Add Clearance and P&L by Date to Dictionary
''--------------------------------------
For i = OF_clearanceColumn1 To OF_clearanceColumn4
currentWeek = Cells(orderStart - 1, i).Value
internalFile.Sheets(currentWeek).Activate
For t = 1 To 700
If Cells(t, 1) = "Station" Then
stationStart = t + 1
Exit For
End If
Next
For t = stationStart To 700
If Cells(t, 1).Value = Cells(stationStart - 2, 1).Value & " Total" Then
stationEnd = i - 1
Exit For
End If
If stationclearanceData.Exists(Cells(t, WT_stationColumn).Value & currentWeek) Then
Else:
On Error Resume Next
stationclearanceData.Add Cells(t, WT_stationColumn).Value & currentWeek, Cells(t, WT_mediaactColumn).Value / Cells(t, WT_mediaestColumn).Value
stationplData.Add Cells(t, WT_stationColumn).Value & currentWeek, Cells(t, WT_profitColumn).Value
End If
Next
orderFile.Activate
Next
Debug.Print Timer - start & " loop through last 4 weeks, add station clearance and P&L data to dictionary"
''--------------------------------------
''Add Cumulative Clearance and P&L to Dictionary
''--------------------------------------
internalFile.Sheets("Cumulative").Activate
For t = 5 To 70000
If Cells(t, 1) = "" And Cells(t + 1, 1) = "" And Cells(t + 2, 1) = "" Then
stationEnd = t + 1
Exit For
End If
Next
For t = 5 To stationEnd
If Cells(t, CT_yearColumn) = 2019 Then
If stationclearanceData.Exists(Cells(t, CT_hashColumn).Value) Then
Else:
On Error Resume Next
stationclearanceData.Add Cells(t, CT_hashColumn).Value, Cells(t, CT_clearanceColumn).Value
stationplData.Add Cells(t, CT_hashColumn).Value, Cells(t, CT_invoiceColumn).Value - Cells(t, CT_actcostColumn).Value
End If
End If
Next
Debug.Print Timer - start & " find range of cumulative, add clearance and P&L for 2019"
orderFile.Activate
''--------------------------------------
''Loop Through Stations on Order File and Update Based on Dictionary Values
''--------------------------------------
For w = orderStart To orderEnd
If Cells(w, OF_stationColumn) <> "" Then
If Cells(w, OF_stationColumn) <> Cells(w - 1, OF_stationColumn) Then
stationHash = Cells(w, OF_stationColumn).Value & " " & Cells(w, OF_trafficColumn).Value & " Total"
On Error Resume Next
Cells(w, OF_clearanceColumn1) = stationclearanceData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_clearanceColumn1).Value)
Cells(w, OF_clearanceColumn2) = stationclearanceData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_clearanceColumn2).Value)
Cells(w, OF_clearanceColumn3) = stationclearanceData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_clearanceColumn3).Value)
Cells(w, OF_clearanceColumn4) = stationclearanceData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_clearanceColumn4).Value)
Cells(w, OF_plColumn1) = stationplData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_plColumn1).Value)
Cells(w, OF_plColumn2) = stationplData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_plColumn2).Value)
Cells(w, OF_plColumn3) = stationplData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_plColumn3).Value)
Cells(w, OF_plColumn4) = stationplData(Cells(w, OF_stationColumn).Value & Cells(orderStart - 1, OF_plColumn4).Value)
Cells(w, OF_totalplColumn) = stationplData(stationHash)
Cells(w, OF_totalclearanceColumn) = stationclearanceData(stationHash)
End If
End If
Next
Debug.Print Timer - start & " adding data from dictionary to order file"
clientdataFile.Activate
ActiveWorkbook.Close saveChanges:=False
Application.ScreenUpdating = True
Range(Cells(orderStart - 2, OF_buyAlgoColumn), Cells(orderEnd, OF_plColumn4)).HorizontalAlignment = xlCenter
MsgBox ("Buy Algorithm Complete")
End Sub