Option Compare Text
Option Explicit
Sub test()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim a As Variant, B As Variant, Key As Variant
Dim k%, i%
Dim ss As Range
Dim ws As Worksheet
Set ws = Sheets("sheet1")
ReDim B(1 To 100000, 1 To 6)
Dim wb As Workbook
Set wb = ThisWorkbook
Application.ScreenUpdating = False
'Array Values from Col A - F
a = ws.Range("a2:f" & ws.Cells(Rows.Count, "a").End(xlUp).Row).Value 'Change if your data is not from a2 to f
With ws
'Copy Column E Values to I then remove duplicates to create unique values
.[i:i].ClearContents
.Range("e2:e" & .Cells(Rows.Count, "E").End(xlUp).Row).Copy .[i1]
.[i:i].RemoveDuplicates Columns:=1, Header:=xlNo
End With
'Loop through unique values and store into dictionary
For Each ss In ws.Range("i1:i" & ws.Cells(Rows.Count, "I").End(xlUp).Row)
k = k + 1
dict.Add ss.Value, k
Next ss
For Each Key In dict.Keys 'Loop through test 2 law test law test 3 law
k = 0
For i = 1 To UBound(a, 1)
If a(i, 4) = Key Or a(i, 5) = Key Then ' If existing
k = k + 1
B(k, 1) = a(i, 1)
B(k, 2) = a(i, 2)
B(k, 3) = a(i, 3)
B(k, 4) = a(i, 4)
B(k, 5) = a(i, 5)
B(k, 6) = a(i, 6)
End If
Next i
Workbooks.Add
With ActiveWorkbook
[a1].Value = "Office"
[b1].Value = "Property"
[c1].Value = "Client"
[d1].Value = "Applicant Solicitor"
[e1].Value = "Client Solicitor"
[f1].Value = "Sale Agreed Date"
[a2].Resize(UBound(B, 1), UBound(B, 2)).Value = B
Columns("A:F").AutoFit
[a1:f1].HorizontalAlignment = xlCenter
[a1:f1].VerticalAlignment = xlCenter
ActiveSheet.Name = Key
.SaveAs Filename:=wb.Path & "\" & Key
.Close
End With
ReDim B(1 To 100000, 1 To 6) 'Clear Array
Next Key
MsgBox "All data has been imported"
Application.ScreenUpdating = True
End Sub