MistakesWereMade
Board Regular
- Joined
- May 22, 2019
- Messages
- 103
Hi all.
I made a program that sorts through a master spread sheet with about 250k data entries. Unfortunately, it takes many hours to run... ANY help in improving the clock time would be tremendously insightful... but also helpful!
My script stores each unique entry name in a column as a vector, and then makes individual workbooks for each of these entries.
HOWEVER, the sorting does not stop here! Each unique entry has at least four categories associated with it, and therefore is sorted once more in finalized workbooks based on the categories.
Here is my all star analogy in case I'm being cryptic.
If I have a column of 4 different chili pepper names, but a single chili pepper may have two different places of origin, I want to sort all the data so that type A chili peppers from North America are in one workbook while type A (or B/C/D) chilli peppers from South America are in a separate workbook.
Thanks for any guidance!
I made a program that sorts through a master spread sheet with about 250k data entries. Unfortunately, it takes many hours to run... ANY help in improving the clock time would be tremendously insightful... but also helpful!
My script stores each unique entry name in a column as a vector, and then makes individual workbooks for each of these entries.
HOWEVER, the sorting does not stop here! Each unique entry has at least four categories associated with it, and therefore is sorted once more in finalized workbooks based on the categories.
Here is my all star analogy in case I'm being cryptic.
If I have a column of 4 different chili pepper names, but a single chili pepper may have two different places of origin, I want to sort all the data so that type A chili peppers from North America are in one workbook while type A (or B/C/D) chilli peppers from South America are in a separate workbook.
Thanks for any guidance!
VBA Code:
Dim d As Object, c As Range, k, tmp As String, rng As Range, x As Long
Dim v As Object, c2 As Range, m, tmp2 As String, rng2 As Range, y As Long
Dim i As Long, ThisWb As Workbook, wbTemp As Workbook, ws As Worksheet
Dim td1 As String, td2 As String, td3 As String, td4 As String
Dim Master As Workbook, n As Long
Dim Z As Integer
Sub GetUniqueAndCount()
' Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
x = Cells(Rows.Count, 5).End(xlUp).Row
Set rng = Range("E2:E" & x)
Set d = CreateObject("scripting.dictionary")
For Each c In rng
tmp = Trim(c.Value)
If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1
Next c
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For Each k In d.keys
Debug.Print k, d(k)
Set ThisWb = ThisWorkbook
Set wbTemp = Workbooks.Add
On Error Resume Next
For Each ws In wbTemp.Worksheets
ws.Delete
Next
On Error GoTo 0
For Each ws In ThisWb.Sheets
ws.Copy After:=wbTemp.Sheets(1)
Next
wbTemp.Sheets(1).Delete
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = Cells(Rows.Count, 5).End(xlUp).Row To 2 Step -1
If Cells(i, 5).Value2 <> k Then
wbTemp.Sheets(1).Rows(i).Delete
End If
Next i
Call LineSort
Next k
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Reset Macro Optimization Settings
ResetSettings:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub LineSort()
y = wbTemp.Sheets(1).Cells(Rows.Count, 12).End(xlUp).Row
Set rng2 = wbTemp.Sheets(1).Range("L2:L" & y)
Set v = CreateObject("scripting.dictionary")
For Each c2 In rng2
tmp2 = Trim(c2.Value)
If Len(tmp2) > 0 Then v(tmp2) = v(tmp2) + 1
Next c2
Set Master = ThisWb
Set ThisWb = wbTemp
For Each m In v.keys
Debug.Print m, v(m)
Set wbTemp = Workbooks.Add
On Error Resume Next
For Each ws In wbTemp.Worksheets
ws.Delete
Next
On Error GoTo 0
For Each ws In ThisWb.Sheets
ws.Copy After:=wbTemp.Sheets(1)
Next
wbTemp.Sheets(1).Delete
Z = m
For n = wbTemp.Sheets(1).Cells(Rows.Count, 12).End(xlUp).Row To 2 Step -1
If wbTemp.Sheets(1).Cells(n, 12).Value <> Z Then
wbTemp.Sheets(1).Rows(n).Delete
End If
Next n
For i = 3 To wbTemp.Sheets(1).Range("E:E").Cells.SpecialCells(xlCellTypeConstants).Count Step 1
wbTemp.Sheets(1).Range("O" & i).Formula = "=TEXT(M" & i & "-M" & i - 1 & ", ""dd:hh:mm:ss"")"
Next i
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 3 To wbTemp.Sheets(1).Range("E:E").Cells.SpecialCells(xlCellTypeConstants).Count Step 1
td1 = Right(wbTemp.Sheets(1).Range("O" & i).Value, 2)
td2 = Mid(wbTemp.Sheets(1).Range("O" & i).Value, 7, 2)
td3 = Mid(wbTemp.Sheets(1).Range("O" & i).Value, 4, 2)
td4 = Left(wbTemp.Sheets(1).Range("O" & i).Value, 2)
td5 = td1 + td2 + td3 + td4
If td5 <> 0 Then
wbTemp.Sheets(1).Range("P" & i).Formula = wbTemp.Sheets(1).Range("H" & i).Value / (td1 / 60 + td2 + td3 * 60 + td4 * 1440)
Else
wbTemp.Sheets(1).Range("P" & i).Formula = 0
End If
Next i
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
wbTemp.SaveAs "C:\Users\hjh\Desktop\hjh\" & k & " Line " & Z & ".xlsx", 51
DoEvents
wbTemp.Close SaveChanges:=True
DoEvents
Next m
ThisWb.Close SaveChanges:=False
Set ThisWb = Master
End Sub