Sub test()
Dim fn(2) As String, a, i As Long, r As Range, x, tbl As ListObject, dic As Object
fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
If fn(0) = "False" Then Exit Sub
fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
If fn(1) = "False" Then Exit Sub
fn(2) = Application.GetSaveAsFilename("CP_" & Format$(Now, "yyyymmmdd hhmmss"), _
"Excel Book,*.xlsx", , "Enter New File Name")
If fn(2) = "False" Then Exit Sub
Application.ScreenUpdating = False
With ThisWorkbook.Sheets(1).Cells(1).CurrentRegion
ReDim myList(1 To 2, 1 To .Columns.Count)
For i = 1 To .Columns.Count
myList(1, i) = .Cells(1, i)
myList(2, i) = Filter(.Parent.Evaluate("transpose(if(" & .Columns(i).Offset(1).Address & _
"<>""""," & .Columns(i).Offset(1).Address & "))"), False, 0)
Next
End With
With Workbooks.Open(fn(0)).Sheets(1).ListObjects(1)
Set r = Union(.ListColumns(2).Range.Resize(, 3), .ListColumns(18).Range)
With Workbooks.Open(fn(1)).Sheets(1)
Set tbl = .ListObjects(1)
For i = 1 To UBound(myList, 2)
myList(2, i) = GetAll(myList(2, i), tbl, myList(1, i))
Next
FilterAndDelete tbl, myList, fn(2)
.Parent.Close False
End With
fn(2) = Mid$(fn(2), InStrRev(fn(2), "\") + 1)
With Workbooks(fn(2)).Sheets(1)
Set tbl = .ListObjects(1)
With tbl
a = Application.Index(.Range, 0, 15)
.Parent.Columns("b:d").Insert
r.Rows(1).Copy .HeaderRowRange.Columns(2)
For i = 2 To UBound(a, 1)
x = Application.Match(a(i, 1), r.Areas(2), 0)
If IsNumeric(x) Then r.Rows(x).Resize(, 3).Copy .ListColumns(2).Range(i)
Next
End With
Application.CutCopyMode = False
.Parent.Close True
End With
.Parent.Parent.Close False
End With
Application.ScreenUpdating = True
End Sub
Sub FilterAndDelete(rng As ListObject, myList, fn As String)
Dim a, x, i As Long, ii As Long, iii As Long, ub As Long, temp
With rng
temp = .Name
If .ShowAutoFilter Then .ShowAutoFilter = False
rng.ShowAutoFilter = True
For i = 1 To UBound(myList, 2)
.Range.AutoFilter myList(1, i), myList(2, i), 7
Next
.Parent.Copy
With ActiveWorkbook.Sheets(1)
.ListObjects(1).Delete
.Parent.SaveAs fn
rng.Range.SpecialCells(12).Copy .[a2]
.ListObjects.Add(1, .[a2].CurrentRegion, 1).Name = temp
.ListObjects(1).TableStyle = ""
Set rng = .ListObjects(1)
End With
End With
With rng
x = Filter(.Parent.Evaluate("transpose(if(" & .ListColumns(4).Range.Address & _
"=""yes""," & .ListColumns(2).Range.Address & "))"), False, 0)
If UBound(x) > -1 Then
.ShowAutoFilter = True
.Range.AutoFilter 2, x, 7
If .ListColumns(1).Range.SpecialCells(12).Count > 1 Then
.DataBodyRange.EntireRow.Delete
End If
End If
.ShowAutoFilter = False
End With
End Sub
Function GetAll(x, tbl As ListObject, ref)
Dim a, ub As Long, i As Long, ii As Long, dic As Object
Set dic = CreateObject("Scripting.Dictionary")
For i = LBound(x) To UBound(x)
If x(i) Like "*[*]*" Then
a = tbl.ListColumns(ref).Range.Value
For ii = 1 To UBound(a, 1)
If a(ii, 1) Like x(i) Then dic(a(ii, 1)) = Empty
Next
If dic.Count Then
ub = UBound(x)
ReDim Preserve x(ub + dic.Count)
For ii = 0 To dic.Count - 1
x(ub + ii + 1) = dic.keys()(ii)
Next
End If
End If
Next
GetAll = x
End Function
Bookmarks