Excel VBA loop code that delete all outlook email files (.msg) located in local windows folder and keep the last email using 2 conditions

Super P

New Member
Joined
May 22, 2021
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I am new in excel VBA, I am looking for an Excel VBA code that will delete all the outlook email files (.msg) saved in local windows folder using 2 conditions and keep the last dated email file. The email files are from different suppliers that supply cement, steel and other construction materials. Each materials have multiple number of suppliers. For example, the first condition is to loop in the folder all the email files that contains a word "Cement" (located in middle of filename) and "Supplier A"(located in the last of the filename next to .msg). Second condition is to delete all the previous dated email and keep the last dated email from the selected files in the first condition e.g. if there are 4 emails found with words in the filename containing "Cement" and "Supplier A", it will delete the 3 previous email and keep the last dated email. I am using cell reference for materials and supplier, the material "Cement" is link in cell A1 and the "Supplier A" is link in cell A2. Appreciate any help, thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top