Option Explicit
Sub DeleteSheet5Data()
Dim ws5 As Worksheet, ws7 As Worksheet
Dim rw5 As Long, rw5Max As Long, col5 As Integer, col5Max As Integer
Dim rw7 As Long, rw7Max As Long, col7 As Integer, col7Max As Integer
Dim ar5 As Variant, ar7 As Variant
Dim bHas7Data As Boolean
Application.ScreenUpdating = False
Set ws5 = ThisWorkbook.Worksheets("Sheet5")
Set ws7 = ThisWorkbook.Worksheets("Sheet7")
rw5Max = ws5.Range("A1048576").End(xlUp).Row
col5Max = ws5.Range("XFD1").End(xlToLeft).Column
rw7Max = ws7.Range("A1048576").End(xlUp).Row
col7Max = ws7.Range("XFD1").End(xlToLeft).Column
ws5.Activate
ar5 = ws5.Range(Cells(1, col5Max + 1), Cells(rw5Max, col5Max + 1))
ws7.Activate
ar7 = ws7.Range(Cells(1, col7Max + 1), Cells(rw7Max, col7Max + 1))
For rw7 = 1 To rw7Max
For col7 = 1 To col7Max
If col7 = 1 Then
ar7(rw7, 1) = ws7.Cells(rw7, col7)
Else
ar7(rw7, 1) = ar7(rw7, 1) & "|" & ws7.Cells(rw7, col7)
End If
Next col7
Next rw7
For rw5 = 1 To rw5Max
For col5 = 1 To col5Max
If col5 = 1 Then
ar5(rw5, 1) = ws5.Cells(rw5, col5)
Else
ar5(rw5, 1) = ar5(rw5, 1) & "|" & ws5.Cells(rw5, col5)
End If
Next col5
bHas7Data = False
For rw7 = 1 To rw7Max
If ar7(rw7, 1) = ar5(rw5, 1) Then
bHas7Data = True
Exit For
End If
Next rw7
If bHas7Data Then
ar5(rw5, 1) = 1
Else
ar5(rw5, 1) = 0
End If
Next rw5
ws5.Activate
ws5.Range(Cells(1, col5Max + 1), Cells(rw5Max, col5Max + 1)) = ar5
ws5.Sort.SortFields.Clear
ws5.Sort.SortFields.Add Key:=Range(Cells(1, col5Max + 1), Cells(rw5Max, col5Max + 1)), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws5.Sort
.SetRange Range(Cells(1, 1), Cells(rw5Max, col5Max + 1))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For rw5 = 1 To rw5Max
If ws5.Cells(rw5, col5Max + 1) = 1 Then
Exit For
End If
Next rw5
ws5.Range(Cells(rw5, 1), Cells(rw5Max, 1)).EntireRow.Delete
ws5.Range(Cells(1, col5Max + 1), Cells(1, col5Max + 1)).EntireColumn.Delete
End Sub