+ Reply to Thread
Results 1 to 4 of 4

macro to copy sheet to end with sheet name current date

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-20-2007
    MS-Off Ver
    2016, 365
    Posts
    127

    macro to copy sheet to end with sheet name current date

    Hi All
    I want to create button macro to copy active sheet to the end with sheet name current date. I create code that copy active sheet and create new workbook:
    Sub CopySheet()
        ActiveSheet.Copy
        ActiveSheet.Name = Format(Date, "mm-dd-yy")
    End Sub
    How to modify the code to get copy active sheet to the end on the same workbook?
    Thanks

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: macro to copy sheet to end with sheet name current date

    Hi eugz,

    Not sure why you'd create a tab for every day but this will do the job:

    Option Explicit
    Sub Macro1()
        
        Dim ws As Worksheet
        
        ActiveSheet.Copy After:=Sheets(Sheets.Count)
        Set ws = Sheets(Sheets.Count)
        ws.Name = Format(Date, "mm-dd-yy")
        
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor
    Join Date
    07-20-2007
    MS-Off Ver
    2016, 365
    Posts
    127

    Re: macro to copy sheet to end with sheet name current date

    Hi Robert. Thanks for reply.
    Your code works awesome. But how to modify it to give ability for user change name in case if user will create some sheets at the same day? How to validate duplicate sheet name? Just inform user that sheet with same name exists and keep sheet name selected to give user ability to modify a sheet name

    Thanks
    Last edited by eugz; 05-09-2022 at 09:47 PM.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: macro to copy sheet to end with sheet name current date

    Try this:

    Option Explicit
    Sub Macro1()
        
        Dim ws As Worksheet
        Dim strTabName As String
        
        Application.ScreenUpdating = False
        
        On Error Resume Next
            Set ws = ThisWorkbook.Sheets(CStr(Format(Date, "mm-dd-yy")))
        On Error GoTo 0
        If Not ws Is Nothing Then
            strTabName = Application.InputBox("There is already a tab called """ & Format(Date, "mm-dd-yy") & """ in the workbook." & vbNewLine & "Please enter a different name:", "Tab Name Editor")
            If strTabName = "False" Then
                Application.ScreenUpdating = True
                Exit Sub
            End If
            Set ws = Nothing
        End If
        
        ActiveSheet.Copy After:=Sheets(Sheets.Count)
        Set ws = Sheets(Sheets.Count)
        ws.Name = IIf(Len(strTabName) > 0, strTabName, Format(Date, "mm-dd-yy"))
        
        Application.ScreenUpdating = True
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy data from one sheet to current sheet instead of named sheet
    By Laguna22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-01-2022, 12:49 PM
  2. Copy Sheet named as current date with button VBA Excel
    By AliceIoana in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-10-2021, 11:18 AM
  3. Macro - copy information from another sheet to current open sheet
    By Haus_i_Hamar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2019, 05:21 AM
  4. [SOLVED] VBA - Macro issue copy/paste line other sheet + duplicate current sheet
    By vcourbiere in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-17-2014, 12:06 PM
  5. Replies: 4
    Last Post: 08-16-2013, 04:10 AM
  6. Replies: 2
    Last Post: 03-14-2012, 03:26 AM
  7. Using a macro to copy current row to another sheet
    By mkalavitz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-19-2005, 11:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1