Option Explicit
Sub MoveEmailsToDeletedItemsFolder()
On Error GoTo errorHandler
Dim sourceWorksheet As Worksheet
Set sourceWorksheet = ThisWorkbook.Worksheets("Sheet1") 'change the sheet name accordingly
Dim EmailsToDelete As Variant
Dim lastRow As Long
With sourceWorksheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
EmailsToDelete = .Range("A1:A" & lastRow).Value
End With
Dim olApp As outlook.Application
Set olApp = New outlook.Application
Dim olNS As outlook.Namespace
Set olNS = outlook.GetNamespace("MAPI")
Dim olInbox As outlook.Folder
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
Dim olDeletedItemsFolder As outlook.Folder
Set olDeletedItemsFolder = olNS.GetDefaultFolder(olFolderDeletedItems)
Dim olItem As Variant
Dim olMailItem As outlook.MailItem
Dim deletedEmailCount As Long
Dim i As Long
deletedEmailCount = 0
With olInbox
For i = .Items.Count To 1 Step -1
If .Items(i).Class = olMail Then
Set olMailItem = .Items(i)
If Not IsError(Application.Match(olMailItem.SenderEmailAddress, EmailsToDelete, 0)) Then
olMailItem.Move olDeletedItemsFolder
deletedEmailCount = deletedEmailCount + 1
End If
End If
Next i
End With
MsgBox "Emails moved to deleted items folder: " & deletedEmailCount, vbInformation
exitHandler:
Set olApp = Nothing
Set olNS = Nothing
Set olInbox = Nothing
Set olDeletedItemsFolder = Nothing
Set olMailItem = Nothing
Exit Sub
errorHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
Resume exitHandler
End Sub