+ Reply to Thread
Results 1 to 6 of 6

How to copy existing worksheet instead of moving values

Hybrid View

Xaos How to copy existing... 12-25-2013, 11:36 AM
fredlo2010 Re: How to copy existing... 12-25-2013, 11:41 AM
Xaos Re: How to copy existing... 12-25-2013, 11:46 AM
berlan Re: How to copy existing... 12-25-2013, 11:51 AM
davesexcel Re: How to copy existing... 12-25-2013, 11:56 AM
Xaos Re: How to copy existing... 12-25-2013, 12:01 PM
  1. #1
    Registered User
    Join Date
    11-24-2007
    Location
    Florida
    MS-Off Ver
    Various
    Posts
    64

    How to copy existing worksheet instead of moving values

    Hello,

    I have a simple macro:

    Sub MG_LOG()
    
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim numpages As Integer
    Dim numweek As Integer
    Dim MG As Integer
    Dim posit As Integer
    
    Set ws1 = Sheet1
    posit = 1
    For MG = 1 To 4
    numweek = 1
        For numpages = 1 To 26
            Set ws2 = ActiveWorkbook.Sheets.Add(, After:=Sheets(Worksheets.Count))
                With ws2
                    .Name = ("MG " & MG & "Weeks" & numweek & "-" & (numweek + 1))
                    .Range("A1:AB21").Value = ws1.Range("A1:AB21").Value
                    .Range("A1").Value = ("Motor Generator " & MG)
                    .Range("A3").Value = ("Week " & numweek)
                    numweek = numweek + 1
                    .Range("A12").Value = ("Week " & numweek)
                    .Range("A23").Offset(0, posit).Value = ("Motor Generator " & MG)
                End With
    numweek = numweek+1
        Next numpages
    posit = posit + 6
    Next MG
    
    End Sub
    It works correctly, but can someone tell me how I can modify this to copy sheet1 (including formatting and pictures) to the output created sheets instead of just moving the values?

    Any help appreciated

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: How to copy existing worksheet instead of moving values

    Hi,

    See if this helps

    Sub MG_LOG()
    
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim numpages As Integer
    Dim numweek As Integer
    Dim MG As Integer
    Dim posit As Integer
    
    Set ws1 = Sheet1
    posit = 1
    For MG = 1 To 4
    numweek = 1
        For numpages = 1 To 26
            Set ws2 = ActiveWorkbook.Sheets.Add(, After:=Sheets(Worksheets.Count))
                With ws2
                    .Name = ("MG " & MG & "Weeks" & numweek & "-" & (numweek + 1))
                     ws1.Range("A1:AB21").Copy Destination:=.Range("A1:AB21")
                    .Range("A1").Value = ("Motor Generator " & MG)
                    .Range("A3").Value = ("Week " & numweek)
                    numweek = numweek + 1
                    .Range("A12").Value = ("Week " & numweek)
                    .Range("A23").Offset(0, posit).Value = ("Motor Generator " & MG)
                End With
    numweek = numweek + 1
        Next numpages
    posit = posit + 6
    Next MG
    
    End Sub
    Thanks

  3. #3
    Registered User
    Join Date
    11-24-2007
    Location
    Florida
    MS-Off Ver
    Various
    Posts
    64

    Re: How to copy existing worksheet instead of moving values

    Thank you! That is almost perfect.

    The only thing that wasn't perserved was the row and column sizes... any ideas on that?

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: How to copy existing worksheet instead of moving values

    Another alternative:

    Sub MG_LOG()
    
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim numpages As Integer
        Dim numweek As Integer
        Dim MG As Integer
        Dim posit As Integer
    
        Set ws1 = Sheet1
        posit = 1
        For MG = 1 To 4
            numweek = 1
            For numpages = 1 To 26
                Set ws2 = ActiveWorkbook.Sheets.Add(, After:=Sheets(Worksheets.Count))
                With ws2
                    ws1.Cells.Copy .Range("A1")
                    .UsedRange.ClearContents
                    .Name = ("MG " & MG & "Weeks" & numweek & "-" & (numweek + 1))
                    .Range("A1").Value = ("Motor Generator " & MG)
                    .Range("A3").Value = ("Week " & numweek)
                    numweek = numweek + 1
                    .Range("A12").Value = ("Week " & numweek)
                    .Range("A23").Offset(0, posit).Value = ("Motor Generator " & MG)
                End With
                numweek = numweek + 1
            Next numpages
            posit = posit + 6
        Next MG
    
    End Sub
    Last edited by berlan; 12-25-2013 at 04:19 PM. Reason: typo

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: How to copy existing worksheet instead of moving values

    Sub MG_LOG()
    
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim numpages As Integer
        Dim numweek As Integer
        Dim MG As Integer
        Dim posit As Integer
    
        Set ws1 = Sheets(1)
        posit = 1
    
        For MG = 1 To 4
    
            numweek = 1
    
            For numpages = 1 To 26
                ws1.Copy after:=Sheets(Sheets.Count)
                Set ws2 = ActiveSheet
    
                With ws2
                    .Name = ("MG " & MG & "Weeks" & numweek & "-" & (numweek + 1))
                    .Range("A1:AB21").Value = ws1.Range("A1:AB21").Value
                    .Range("A1").Value = ("Motor Generator " & MG)
                    .Range("A3").Value = ("Week " & numweek)
                    numweek = numweek + 1
                    .Range("A12").Value = ("Week " & numweek)
                    .Range("A23").Offset(0, posit).Value = ("Motor Generator " & MG)
                End With
    
                numweek = numweek + 1
    
            Next numpages
    
            posit = posit + 6
    
        Next MG
    
    End Sub

  6. #6
    Registered User
    Join Date
    11-24-2007
    Location
    Florida
    MS-Off Ver
    Various
    Posts
    64

    Re: How to copy existing worksheet instead of moving values

    Excellent.

    Now I see what I was doing wrong. I was trying to set ws2 and copy in the same statement.

    Thank you all for the help and Happy Holidays!

+ 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. Replies: 11
    Last Post: 11-04-2013, 04:32 PM
  2. Replies: 3
    Last Post: 10-14-2013, 03:06 PM
  3. [SOLVED] Copy worksheet from one WB to another, overwrite existing worksheet with same CodeName
    By Renns in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 08-31-2012, 10:30 AM
  4. Macro to create new worksheet with values from existing worksheet
    By SamBam in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2011, 05:33 AM
  5. Copy an existing worksheet and carry totals to a master worksheet
    By adore_r in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2008, 12:02 AM

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