Results 1 to 9 of 9

Thread: [RESOLVED][VBA] Help with macro to copy worksheet

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    Resolved [RESOLVED][VBA] Help with macro to copy worksheet

    Hi All,
    I'm trying to create a macro that copies the current worksheet and places the copied ws before it. I've gotten as far as below, but that only copies the ws and places it in the second to last tab. Thanks in advance for any help.


    Sub Copysheet()

    Dim i As Integer
    Dim p As Integer
    On Error GoTo out
    i = InputBox("How many copies would you like?", "Making Copies")

    Application.ScreenUpdating = False
    p = 0
    Do
    ActiveSheet.Copy Before:=Sheets(Sheets.Count)
    p = p + 1
    Loop Until p = i
    Application.ScreenUpdating = True
    Exit Sub
    out:
    MsgBox "Copy Cancelled"
    Application.ScreenUpdating = True
    End Sub
    Last edited by newtovba; Feb 8th, 2012 at 03:45 PM. Reason: Better heading

  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    17

    Re: [VBA] Help with macro to copy worksheet

    Change
    ActiveSheet.Copy Before:=Sheets(Sheets.Count)
    to
    ActiveSheet.Copy Before:=Sheets("NameofOriginalWorksheet")

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    Re: [VBA] Help with macro to copy worksheet

    I cant seem to get it to work. It goes to "copy cancelled"
    Last edited by newtovba; Feb 8th, 2012 at 03:03 PM. Reason: spelling

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    17

    Re: [VBA] Help with macro to copy worksheet

    Post the amended code for me.

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [VBA] Help with macro to copy worksheet

    @LexGixxer
    I take a guess, he did paste in exactly what you posted!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    Re: [VBA] Help with macro to copy worksheet

    I should have said it does work, but by taking the name of worksheet, it will only allow me to copy that one specific worksheet over (I need to copy the current ws (which is then renamed and updated), and then copied again at a later date


    Sub Copysheet()

    Dim i As Integer
    Dim p As Integer
    On Error GoTo out
    i = InputBox("How many copies would you like?", "Making Copies")

    Application.ScreenUpdating = False
    p = 0
    Do
    ActiveWorkbook.Sheets("February 12").Copy Before:=ActiveWorkbook.Sheets("February 12")
    p = p + 1
    Loop Until p = i
    Application.ScreenUpdating = True
    Exit Sub
    out:
    MsgBox "Copy Cancelled"
    Application.ScreenUpdating = True
    End Sub

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [VBA] Help with macro to copy worksheet

    If need to work with the ActiveSheet just use ".ActiveSheet" instead of ".Sheets(xxxx)".
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    4

    Re: [VBA] Help with macro to copy worksheet

    Got it. Thanks.

    A little new at this

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [VBA] Help with macro to copy worksheet

    (I need to copy the current ws (which is then renamed and updated), and then copied again at a later date
    how can the code know when these changes have been made and to now copy the worksheet?
    unless you make these changes within the code, then copy again
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width