+ Reply to Thread
Results 1 to 11 of 11

Copy Sheet named as current date with button VBA Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    06-10-2021
    Location
    romania
    MS-Off Ver
    2013
    Posts
    5

    Unhappy Copy Sheet named as current date with button VBA Excel

    I am trying to copy the first sheet once/click and name it automatically as current date.

    first click is adding 2 sheets (10-Jun-21 & 10-Jun-21 (2));
    Second click is adding Operator(2) & 10-Jun-21 (3);
    third click is adding Operator(3) & 10-Jun-21(4);
    When i click the button 4th time is adding one sheet per click (10-Jun-21(5)).
    I don't understand why after the 4th click does what i want it to do. Here is my code. Thank you!!!

    Private Sub CommandButton1_Click()
    
    Dim I As Long
    Dim szTodayDate As String
        szTodayDate = Format(Date, "mmm-dd-yyyy")
        On Error Resume Next
    
    Sheets("Operator").Copy After:=Worksheets(Worksheets.Count)
    Do
    ActiveSheet.Name = szTodayDate
        Sheets(szTodayDate).Copy After:=Worksheets(Worksheets.Count)
        ActiveSheet.Name = szTodayDate
    
    Loop While ActiveSheet.Name = szTodayDate
    
    End Sub
    Last edited by AliceIoana; 06-10-2021 at 08:48 AM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Copy Sheet named as current date with button VBA Excel

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code] [/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Copy Sheet named as current date with button VBA Excel

    Try:
    Private Sub CommandButton1_Click()
        Dim I As Long, szTodayDate As String
        szTodayDate = Format(Date, "mmm-dd-yyyy")
        If Not Evaluate("isref('" & szTodayDate & "'!A1)") Then
            Sheets("Operator").Copy After:=Worksheets(Worksheets.Count)
            ActiveSheet.Name = szTodayDate
        Else
            Sheets(szTodayDate).Copy After:=Worksheets(Worksheets.Count)
        End If
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Registered User
    Join Date
    06-10-2021
    Location
    romania
    MS-Off Ver
    2013
    Posts
    5

    Re: Copy Sheet named as current date with button VBA Excel

    I have one more question.

    I saw that for example if i use the button from the 3rd added sheet, it copies the 2nd sheet, which already has information in it.

    is there a way to copy the main sheet? I am a beginner and i dont have much information to use

  5. #5
    Registered User
    Join Date
    06-10-2021
    Location
    romania
    MS-Off Ver
    2013
    Posts
    5

    Re: Copy Sheet named as current date with button VBA Excel

    It worked. THANK YOU!! You are a life saver xD

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Copy Sheet named as current date with button VBA Excel

    You are very welcome.

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Copy Sheet named as current date with button VBA Excel

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific sheets (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file.

  8. #8
    Registered User
    Join Date
    06-10-2021
    Location
    romania
    MS-Off Ver
    2013
    Posts
    5

    Re: Copy Sheet named as current date with button VBA Excel

    First time I will click the button from sheet 1, which will remain the main page, unchanged (template if i can say).

    after i introduce the data in second sheet(the copied one), i will press the button from there. And automatically will copy Sheet2, not Sheet1 as i want to.

    so everytime I press the button, doesn't matter from which Sheet, I want to copy Sheet1, the main page.

    I hope it makes sense
    Attached Files Attached Files

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Copy Sheet named as current date with button VBA Excel

    Try the following work-around:

    Enter a value of 1 in cell AA1of the "Operator handover log new" sheet. This cell will be used as a "helper" cell to keep track of the sheet numbers and should never be deleted. Then try this macro:
    Private Sub CommandButton1_Click()
        Dim szTodayDate As String
        szTodayDate = Format(Date, "mmm-dd-yyyy")
        Sheets("Operator handover log new").Copy After:=Worksheets(Worksheets.Count)
        ActiveSheet.Range("AA1").ClearContents
        ActiveSheet.Name = szTodayDate & " (" & Sheets("Operator handover log new").Range("AA1") & ")"
        Sheets("Operator handover log new").Range("AA1") = Sheets("Operator handover log new").Range("AA1") + 1
    End Sub

  10. #10
    Registered User
    Join Date
    06-10-2021
    Location
    romania
    MS-Off Ver
    2013
    Posts
    5

    Re: Copy Sheet named as current date with button VBA Excel

    You are a god. Thank you so much for your help and TIME!!!

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Copy Sheet named as current date with button VBA Excel

    My pleasure.

+ 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. Button to open up existing excel file in another sheet in current excel workbook
    By bearcats667 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-29-2018, 02:58 PM
  2. [SOLVED] Button to copy the current cell into the next blank cell of another sheet
    By Smeghead in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-23-2015, 08:38 AM
  3. Replies: 4
    Last Post: 08-16-2013, 04:10 AM
  4. command button 1 (save as pdf named the current time and date . then send by email
    By guitarist00 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-02-2013, 09:43 AM
  5. Using VBA button to "Save as", add current date & copy cell data.
    By Pedro1803 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-26-2011, 07:56 PM
  6. Copy a sheet into a new book and save file name as the current date.
    By Jay3 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 07-10-2009, 06:59 AM
  7. Command button - to copy active sheet and not save current workbook
    By vjboaz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2008, 05:23 PM

Tags for this Thread

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