Excel Guides

Self-Deleting Macros in Excel

Self-deleting macros are a handy tool to have in your Excel arsenal. They can be used to clean up unwanted data, remove old files, or simply make space on your hard drive. Here's how to create a self-deleting macro:

  1. Open the Visual Basic Editor by pressing Alt+F11 on your keyboard. If you've never used the VBE before, don't worry - it's easy!
  2. In the VBE, create a new module by clicking Insert > Module. A new module will appear in the pane on the left.
  3. Paste the following code into the new module:
Sub DeleteMe() 
 On Error Resume Next 
 Kill "C:\path\to\file\you\want\deleted" 
 On Error GoTo 0 
End Sub

Replace "C:\path\to\file\you\want\deleted" with the path of the file you want to delete. Make sure to use double backslashes (\\) instead of single backslashes (\) - otherwise, the macro won't work!

  1. Save and close the VBE.
  2. Now, whenever you want to delete the file, just run the macro by pressing Alt+F8 on your keyboard and selecting DeleteMe from the list of macros.

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.