Option Explicit
Sub testclear()
Dim wsIn As Worksheet, wsOut As Worksheet
Dim LastRow As Long, RowNo As Long
Dim LastCol As Long, ColNo As Long
Dim strFmla1 As String, strFmla2 As String, strFmla3 As String, strFmla4 As String
Set wsIn = Sheets("DataIn")
Set wsOut = Sheets("Combined")
With wsIn
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(2, 1), .Cells(LastRow, LastCol)).Sort Key1:=Range("B1"), Order1:=xlAscending, _
Key2:=Range("C1"), Order2:=xlAscending
End With
strFmla1 = "=IF(B2&C2=B3&C3," & Chr(34) & "Delete" & Chr(34) & ","""")"
strFmla2 = "=IF(B2&C2=B1&C1,0,IF(B2&C2=B3&C3," & Chr(34) & "Delete" & Chr(34) & ",""""))"
strFmla3 = "=IF(I2="""","""",IF(I2=" & Chr(34) & "Delete" & Chr(34) & ",I2,IF(ISNUMBER(J1),J1+F2,F1+F2)))"
strFmla4 = "=IF(H2=" & Chr(34) & "Delete" & Chr(34) & ",H2,IF(J2="""",F2,J2))"
With wsIn
.Range(.Cells(2, LastCol + 1).Address).Resize(LastRow - 1, 1).Formula = strFmla1
.Range(.Cells(2, LastCol + 2).Address).Resize(LastRow - 1, 1).Formula = strFmla2
.Range(.Cells(2, LastCol + 3).Address).Resize(LastRow - 1, 1).Formula = strFmla3
.Range(.Cells(2, LastCol + 4).Address).Resize(LastRow - 1, 1).Formula = strFmla4
.Range(.Cells(2, LastCol + 4).Address).EntireColumn.Copy
.Range(.Cells(1, LastCol + 4).Address).PasteSpecial xlValues
.Range(.Cells(2, LastCol + 1), .Cells(2, LastCol + 3)).EntireColumn.Delete
.Range(.Cells(1, 1), .Cells(1, LastCol + 1)).EntireColumn.Copy wsOut.Range("A1")
.Range(.Cells(2, LastCol + 1).Address).EntireColumn.Clear
End With
With wsOut
.Range(.Cells(2, LastCol + 1), .Cells(LastRow, LastCol + 1)).Copy
.Range("F2").PasteSpecial xlValues
.Range(.Cells(2, LastCol + 1), .Cells(LastRow, LastCol + 1)).Clear
.Range(.Cells(1, 1), .Cells(LastRow, LastCol)).AutoFilter Field:=6, Criteria1:="Delete"
.Rows("2:" & LastRow).Delete Shift:=xlUp
.Range(.Cells(1, 1), .Cells(LastRow, LastCol)).AutoFilter
End With
End Sub
Bookmarks