Consulting

Results 1 to 12 of 12

Thread: VBA to copy a new sheet based on active sheet

  1. #1
    VBAX Regular
    Joined
    Jul 2017
    Posts
    23
    Location

    VBA to copy a new sheet based on active sheet

    I am trying to copy a new sheet based on the active sheet and rename it using an input box. I then want to copy a sheet named "January" and copy and paste that data into the worksheet I just created, but it needs to be a variable named worksheet as I add a new sheet each month.
    In the code below, "March" in this case will be named to April for the following month.
    I hope this explanation makes sense. I can provide the workbook if needed.

    Can someone please help. Here is what I have so far.
    Thanks in advance,
    Ken Mc

    Sub InsertDuplicateSheet()
        ActiveSheet.Copy After:=ActiveSheet
        ActiveSheet.Name = InputBox("Please insert the name of the new sheet.", "Rename Sheet")
        Sheets("January").Select
        Range("A1").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("March").Select 
        Range("A1").Select
        ActiveSheet.Paste
        Range("B4").Select
        
    End Sub

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Posts
    55
    Location
    You copy all of the cells in the sheet "January" to the new sheet, so why don't you copy the sheet "January" right from the beginning?

  3. #3
    VBAX Regular
    Joined
    Jul 2017
    Posts
    23
    Location
    Because I add a sheet each month and I don't want to have to go in and change the code to "April", "May", and so on, thus, I wanted to know if there was a way to avoid that.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    How about checking to see if "February" exists and then copy "January" as "March", etc.?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Jul 2017
    Posts
    23
    Location
    Thanks Paul but I'm not understanding you. Yes, "Feb" exists, but I would still have to modify my code, right?

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Like this


    Option Explicit
    
    Sub etc()
        Dim sNextMonth As String, sMonth As String
        Dim i As Long, n As Long
        
        
        For i = 1 To 12
            sMonth = Format(DateSerial(2018, i, 1), "mmmm")
            n = -1
            On Error Resume Next
            n = Worksheets(sMonth).Index
            On Error GoTo 0
            
            'if worksheet sMonth does not exist then sMonth the next month
            If n = -1 Then
                sNextMonth = sMonth
                Exit For
            End If
        Next i
        
        MsgBox sNextMonth
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Jul 2017
    Posts
    23
    Location
    Thanks Paul, but I'm not sure how to use this to select the next month to paste using the code you sent.
    Somehow I need to replace this part of the code using a variable to select the sheet I just created, ie....."March"
    Sheets("March").Select
    Range("A1").Select
    ActiveSheet.Paste
    Range("B4").Select

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Because I add a sheet each month and I don't want to have to go in and change the code to "April", "May", and so on, thus, I wanted to know if there was a way to avoid that.

    If (very big IF) I'm understanding what you want to do, this copies the ActiveSheet and renames it the next available month

    If Jan exist in the workbook, the copy is named Feb. If Feb exists the copy is named March, etc.


    Option Explicit
    
    Sub CopyCurrentSheetAndBumpMonth()
        Dim sNextMonth As String, sMonth As String
        Dim i As Long, n As Long
         
         
        For i = 1 To 12
            sMonth = Format(DateSerial(2018, i, 1), "mmmm")
            n = -1
            On Error Resume Next
            n = Worksheets(sMonth).Index
            On Error GoTo 0
             
             'if worksheet sMonth does not exist then sMonth the next month
            If n = -1 Then
                sNextMonth = sMonth
                Exit For
            End If
        Next I
         
        ActiveSheet.Copy after:=ActiveSheet
        ActiveSheet.Name = sNextMonth
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Regular
    Joined
    Jan 2018
    Posts
    55
    Location
    ActiveSheet.Name = InputBox("Please insert the name of the new sheet.", "Rename Sheet")
    In your code, do you enter "March" with this input box?

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    No -- there is no need for an InputBox

    If the sheet "February" exists then the copy is automatically named "March"
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Do you really need to manually enter the next month?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    VBAX Regular
    Joined
    Jan 2018
    Posts
    55
    Location
    Do you really need to manually enter the next month?
    I have the same question.

Posting Permissions

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