Appending Date on end of WorkSheet

caleb06

New Member
Joined
Feb 4, 2016
Messages
2
Hi everyone,

This has been posted before, however, this has a slight twist on it. I am trying to set up a VBA macro so when I click on the "Save" button it copies the current excel sheet and saves it as a date.

I don't want it to save as a completely new excel file, I want it to save as a new excel worksheet inside the same file.

I believe I am close but can't seem to figure out why it won't work.

Code:
Sub Button1_Click()

    Dim dt As String
    dt = Format(Now(), "DD-MM-YYYY")
    Set wb = ThisWorkbook
    ThisWorkbook.Activate
    ActiveSheet.Copy After:=wb.dt
    wb.Activate
    MsgBox "Saved as " + dt
    
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the Board!

You don't really save a new worksheet as an individual element in a workbook as they're all part of the workbook, so you can only save the whole thing.

Code:
    Set wb = ThisWorkbook
    ThisWorkbook.Activate

Your current code has issues in that you don't need to set a reference to the current wb since you're not switching between a different one.

See if this gets you started:

Code:
Sub Button1_Click()
    Dim dt As String
        dt = Format(Now(), "DD-MM-YYYY")
            ActiveSheet.Copy After:=Sheets(Sheets.Count)
            ActiveSheet.Name = dt
        '   You haven't saved anything yet, so probably don't need the MsgBox
            MsgBox "Saved as " + dt
End Sub

Note there's no error handling in case the sheet name already exists, so it will blow up if there's a conflict.
 
Last edited:
Upvote 0
Welcome to the Board!

You don't really save a new worksheet as an individual element in a workbook as they're all part of the workbook, so you can only save the whole thing.

Code:
    Set wb = ThisWorkbook
    ThisWorkbook.Activate

Your current code has issues in that you don't need to set a reference to the current wb since you're not switching between a different one.

See if this gets you started:

Code:
Sub Button1_Click()
    Dim dt As String
        dt = Format(Now(), "DD-MM-YYYY")
            ActiveSheet.Copy After:=Sheets(Sheets.Count)
            ActiveSheet.Name = dt
        '   You haven't saved anything yet, so probably don't need the MsgBox
            MsgBox "Saved as " + dt
End Sub

Note there's no error handling in case the sheet name already exists, so it will blow up if there's a conflict.

Thanks, this works a treat!
Won't have to worry about the error handling issue as this sheet will only be used once monthly.
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,861
Members
449,411
Latest member
adunn_23

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