Sub CompareSheetsAndFindMissing()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRow1 As Long, lastRow2 As Long
Dim dict As Object
Dim cell As Range
Dim resultSheet As Worksheet
' Set the sheets
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("RESPONSE")
' Find the last rows
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
' Create a dictionary to store Session IDs from RESPONSE
Set dict = CreateObject("Scripting.Dictionary")
For Each cell In ws2.Range("A2:A" & lastRow2)
dict(cell.Value) = True
Next cell
' Create a new sheet for results
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Missing Data").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Set resultSheet = ThisWorkbook.Sheets.Add
resultSheet.Name = "Missing Data"
' Write header
resultSheet.Range("A1").Value = "Session ID"
resultSheet.Range("B1").Value = "Amount"
resultSheet.Range("C1").Value = "Service Charge"
' Check for missing IDs in RESPONSE
Dim resultRow As Long
resultRow = 2
For Each cell In ws1.Range("A2:A" & lastRow1)
If Not dict.exists(cell.Value) Then
resultSheet.Cells(resultRow, 1).Value = cell.Value
resultSheet.Cells(resultRow, 2).Value = ws1.Cells(cell.Row, 2).Value
resultSheet.Cells(resultRow, 3).Value = ws1.Cells(cell.Row, 3).Value
resultRow = resultRow + 1
End If
Next cell
' Sort results by Amount (Column B) in descending order
With resultSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=resultSheet.Range("B2:B" & resultRow - 1), _
SortOn:=xlSortOnValues, Order:=xlDescending,
DataOption:=xlSortNormal
.SetRange resultSheet.Range("A1:C" & resultRow - 1)
.Header = xlYes
.Apply
End With
MsgBox "Comparison complete. Missing data saved to 'Missing Data' sheet.",
vbInformation
End Sub