How can I delete a recently created excel sheet using VBA

Cmack

New Member
Joined
May 23, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have struggled to generate a form that can be updated with a button click.
The program generates a DateTime stamp then copies the sheet to a file
My issue is that I want to delete the sheet once saved to file and return to the original but I keep getting errors (no matter which method I try).
Any advice or information pertaining to a solution is greatly appreciated.
Cheers in advance,
Cmack.

Private Sub CommandButton2_Click()
Application.CalculateFull 'sets the clock
Application.ScreenUpdating = True 'updates the screen
Dim FName As String 'declares file name

ActiveSheet.Copy 'copies original
With ActiveSheet.Range("A1") ' correct date and time
.Copy
.PasteSpecial xlValues
.PasteSpecial xlFormats

Application.CutCopyMode = False
Application.ScreenUpdating = False
End With
Application.DisplayAlerts = False 'no alerts

FName = "C:\Users\cmack\OneDrive\Desktop\Testing\" & Format(Range("A1"), "hh-MM--ss-mmm-d-yyyy") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlOpenXMLWorkbookMacroEnabled 'saves the new copy (date stamped title) into folder


??????????? How to delete the newly copied sheet?????????
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
??????????? How to delete the newly copied sheet?????????
What about instead of deleting a worksheet, you close the new workbook by adding this line at the end of the sub:

VBA Code:
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlOpenXMLWorkbookMacroEnabled 'saves the new copy (date stamped title) into folder
ActiveWorkbook.Close False  '<-- new line
End Sub

(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

 
Upvote 1
Solution
What about instead of deleting a worksheet, you close the new workbook by adding this line at the end of the sub:

VBA Code:
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlOpenXMLWorkbookMacroEnabled 'saves the new copy (date stamped title) into folder
ActiveWorkbook.Close False  '<-- new line
End Sub

(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

Thank you for your support, great idea, so simple and it worked immediately, nil issues. This was my first question, not sure what a code tag is yet but I hope to get there.
Thank you for your time my friend.
Cmack
 
Upvote 0
Lol, I clicked my own reply as a solution hahahaha
 
Upvote 0

Forum statistics

Threads
1,214,541
Messages
6,120,110
Members
448,945
Latest member
Vmanchoppy

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