Code:
Sub kTest_v1()
Dim k, Data As Range, x, Hdr, i As Long, Crits, s As String
Dim wbkActive As Workbook, wbkNew As Workbook, j As Long
Dim FN As String, Cols, TltName As String
With Application.FileDialog(3)
.AllowMultiSelect = False
.Title = "Select the Template File"
.Filters.Add "Excel Template", ("*.xltx;*.xlt")
.InitialFileName = ThisWorkbook.Path
If .Show = -1 Then
TltName = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set wbkActive = ThisWorkbook
Hdr = Array("Gateway", "Supplier Code", "Formulated Column", "Supplier Code", _
"Reference", "Amount (£)", "Doc Currency", "Country", "Invoice Number")
Cols = Array(3, 6, , 7, 5, 8, 9, 11, 4)
Application.ScreenUpdating = False
With wbkActive.Sheets("workings")
If .AutoFilterMode Then .AutoFilterMode = False
Set Data = .Range("a1:l" & .Range("a" & .Rows.Count).End(xlUp).Row)
k = Data.Value2
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For i = 2 To UBound(k, 1)
If Len(k(i, 2)) Then
s = k(i, 2) & "|" & k(i, 9) & "|" & k(i, 11)
If Not .exists(s) Then
.Add s, Nothing
End If
End If
Next
k = .keys
End With
With Data
For i = 0 To UBound(k)
x = Split(k(i), "|")
.AutoFilter 2, x(0), 1
.AutoFilter 9, x(1), 1
.AutoFilter 11, x(2)
FN = .Offset(1).Cells(1, .Columns.Count).Value
Set wbkNew = Workbooks.Open(TltName)
With wbkNew.Sheets(1)
.Range("a2:a4") = Application.Transpose([{"File Number","Payment/Recovery","Currency"}])
.Range("a7").Resize(, UBound(Hdr) + 1) = Hdr
.Range("b2") = FN
.Range("b3") = x(0)
.Range("b4") = x(1)
End With
On Error Resume Next
For j = 0 To UBound(Cols)
.Columns(Cols(j)).Copy wbkNew.Sheets(1).Cells(8, j + 1)
Next
On Error GoTo 0
wbkNew.Sheets(1).Rows(8).Delete
wbkNew.SaveAs wbkActive.Path & "\" & FN & "-" & x(0) & "-" & x(1) & "-" & x(2), 51
wbkNew.Close
Set wbkNew = Nothing
Next
.Parent.AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
Bookmarks