Creating a Document in same folder when another document is opened

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
OK So heres what I need,
I need two macros,
1 when the document is opened it creates a new document in the same folder called "Used"
When the document is closed it deletes the docment called "Used"

how can I do this.

Thanks

Tony
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
place code in ThisWorkbook module
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    DeleteUsed
End Sub

Private Sub Workbook_Open()
    DeleteUsed
    CreateUsed
End Sub

Private Sub DeleteUsed()
    On Error Resume Next
    Kill ThisWorkbook.Path & "\Used.xlsx"
End Sub

Private Sub CreateUsed()
    Application.ScreenUpdating = False
    Dim wb As Workbook
    Set wb = Workbooks.Add
    Application.DisplayAlerts = False
    wb.SaveAs Filename:=ThisWorkbook.Path & "\Used.xlsx"
    Application.DisplayAlerts = True
    wb.Close False
End Sub

:warning: VBA will not do what you require if placed in incorrect module
 
Last edited:
Upvote 0
1 when the document is opened it creates a new document in the same folder called "Used"
When the document is closed it deletes the docment called "Used"
Word uses documents, Excel uses workbooks. To avoid confusion, please use the standard MS Office terminology.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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