Hello Benajmin2008,
Sorry for the late response. This was a tricky problem. The biggest issue I had with writing the macro was execution speed. I tried many different ways and this version gave me the best performance. Add this macro to a separate VBA module in your workbook. I am still unable to upload files.
Sub ReduceData()
Dim DocType As Object
Dim DstWks As Worksheet
Dim Item As Variant
Dim R As Long
Dim Rng As Range
Dim SrcWks As Worksheet
Set SrcWks = Worksheets("QuerySheet")
R = 2 'Header Row on destination sheet
Set DocType = CreateObject("Scripting.Dictionary")
DocType.CompareMode = vbTextCompare
With DocType
.Add "AP Documents", 1
.Add "Non PO Invoice", 2
.Add "PO Invoice", 3
End With
Set Rng = SrcWks.Range("B8").CurrentRegion
'Delete the Results worksheet
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("Results").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Make s copy of the QuerySheet and rename it Results
SrcWks.Copy After:=Worksheets(Worksheets.Count)
Set DstWks = ActiveSheet
DstWks.Name = "Results"
'Remove the merged header cells in rows 1 o 7
DstWks.Range("1:7").Delete
DstWks.Cells(1, 1).EntireRow.Insert
'Set data range to Skip the header row (QuerySheet)
Set Rng = Rng.Offset(1, 0).Resize(RowSize:=Rng.Rows.Count - 1)
'Clear the destination data range
DstWks.Cells(R + 1, 2).Resize(Rng.Rows.Count, Rng.Columns.Count).ClearContents
'Copy only data matching the document type
For Each Item In Rng.Columns(2).Cells
If DocType.Exists(Item.Value) Then
R = R + 1
DstWks.Rows(R) = Item.EntireRow.Value
End If
Next Item
'Remove the unneeded columns on Results
DstWks.Range("X:AN,T:V,R:R,O:O,G:L").Delete
DstWks.Cells(2, 2).Select
Set DocType = Nothing
End Sub
Bookmarks