Code:
Sub Consolidator()
Application.ScreenUpdating = False
Dim rngPosition As Range, rngAccounts As Range, rngHistory As Range
Dim rngA As Range, rngP As Range, rngH As Range
Dim strPeriodCriteria As String, strPreQCriteria As String
Dim objDic As Object: Set objDic = CreateObject("Scripting.Dictionary") ' New Dictionary
With Worksheets("Sheet5")
Set rngPosition = ThisWorkbook.Sheets("All Positions, All Accounts Mar").Range("PositionsTable")
Set rngAccounts = .Range("SampAccounts")
Set rngHistory = ThisWorkbook.Sheets("ALL HISTORY, ALL ACCOUNTS").Range("History")
strPeriodCriteria = .Range("B1").Value & .Range("B2").Value
If .Range("B1").Value = 1 Then
strPreQCriteria = "4" & .Range("B2").Value - 1
Else
strPreQCriteria = .Range("B1").Value - 1 & .Range("B2").Value
End If
For Each rngA In rngAccounts.Columns(1).Cells
For Each rngP In rngPosition.Columns(5).Cells
If rngP.Value = rngA.Value Then
If rngP.Offset(, 13).Value & rngP.Offset(, 15).Value = strPeriodCriteria Then
objDic.Item(rngP.Value & "|" & rngP.Offset(, 3).Value) = 0
ElseIf rngP.Offset(, 13).Value & rngP.Offset(, 15).Value = strPreQCriteria Then
objDic.Item(rngP.Value & "|" & rngP.Offset(, 3).Value) = 0
End If
End If
Next rngP
For Each rngH In rngHistory.Columns(6).Cells
If rngH.Value = rngA.Value Then
If Replace(Mid(rngH.Offset(, 18), 2), " ", "") = strPeriodCriteria Then
objDic.Item(rngH.Value & "|" & rngH.Offset(, 1).Value) = 0
ElseIf rngH.Offset(, 16).Value & rngH.Offset(, 17).Value & Replace(Mid(rngH.Offset(, 18), 2), " ", "") = strPeriodCriteria Then
objDic.Item(rngH.Value & "|" & rngH.Offset(, 1).Value) = 0
End If
End If
Next rngH
Next rngA
.Range("Orig").Offset(1).ClearContents
.ListObjects("Orig").Resize .Range("$A$5:$I$6")
.Range("Orig").Range("A1").Resize(objDic.Count).Value = Application.Transpose(objDic.Keys)
Application.DisplayAlerts = 0
.Range("Orig").Columns(1).Cells.TextToColumns _
Destination:=.Range("A6"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:="|", _
FieldInfo:=Array(Array(1, 1), Array(2, 1)), _
TrailingMinusNumbers:=True
Application.DisplayAlerts = 1
Range("B:B").Replace "*~*EXCHANGED*", "", xlPart
Range("B:B").Replace "CASH", "ZZZCASH", xlPart
End With
With Worksheets("Sheet5").ListObjects("Orig").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("Orig[Account]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("Orig[Position]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With Worksheets("Sheet5")
Range("B:B").Replace "ZZZCASH", "CASH", xlPart
End With
'With ActiveSheet.ListObjects("Orig")
'.Range.AutoFilter Field:=2, Criteria1:="="
'.DataBodyRange.EntireRow.Delete
'.Range.AutoFilter Field:=2
'End With
Application.ScreenUpdating = True
End Sub
Bookmarks