Copy an entire worksheet to a new worksheet in Excel 2010

前端 未结 6 1425
长发绾君心
长发绾君心 2020-12-01 10:28

I have found similar questions that deal with copying an entire worksheet in one workbook and pasting it to another workbook, but I am interested in simply copying an entire

相关标签:
6条回答
  • 2020-12-01 11:08
    ' Assume that the code name the worksheet is Sheet1
    
    ' Copy the sheet using code name and put in the end.
    ' Note: Using the code name lets the user rename the worksheet without breaking the VBA code
    Sheet1.Copy After:=Sheets(Sheets.Count)
    
    ' Rename the copied sheet keeping the same name and appending a string " copied"
    ActiveSheet.Name = Sheet1.Name & " copied"
    
    0 讨论(0)
  • 2020-12-01 11:15
    ThisWorkbook.Worksheets("Master").Sheet1.Cells.Copy _
        Destination:=newWorksheet.Cells
    

    The above will copy the cells. If you really want to duplicate the entire sheet, then I'd go with @brettdj's answer.

    0 讨论(0)
  • 2020-12-01 11:15
    'Make the excel file that runs the software the active workbook
    ThisWorkbook.Activate
    
    'The first sheet used as a temporary place to hold the data 
    ThisWorkbook.Worksheets(1).Cells.Copy
    
    'Create a new Excel workbook
    Dim NewCaseFile As Workbook
    Dim strFileName As String
    
    Set NewCaseFile = Workbooks.Add
    With NewCaseFile
        Sheets(1).Select
        Cells(1, 1).Select
    End With
    
    ActiveSheet.Paste
    
    0 讨论(0)
  • 2020-12-01 11:17

    If anyone has, like I do, an Estimating workbook with a default number of visible pricing sheets, a Summary and a larger number of hidden and 'protected' worksheets full of sensitive data but may need to create additional visible worksheets to arrive at a proper price, I have variant of the above responses that creates the said visible worksheets based on a protected hidden "Master". I have used the code provided by @/jean-fran%c3%a7ois-corbett and @thanos-a in combination with simple VBA as shown below.

    Sub sbInsertWorksheetAfter()

        'This adds a new visible worksheet after the last visible worksheet
    
        ThisWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
    
        'This copies the content of the HIDDEN "Master" worksheet to the new VISIBLE ActiveSheet just created
    
        ThisWorkbook.Sheets("Master").Cells.Copy _
            Destination:=ActiveSheet.Cells
    
        'This gives the the new ActiveSheet a default name
    
        With ActiveSheet
            .Name = Sheet12.Name & " copied"
        End With
    
        'This changes the name of the ActiveSheet to the user's preference
    
        Dim sheetname As String
    
        With ActiveSheet
            sheetname = InputBox("Enter name of this Worksheet")
            .Name = sheetname
        End With
    

    End Sub

    0 讨论(0)
  • 2020-12-01 11:24

    It is simpler just to run an exact copy like below to put the copy in as the last sheet

    Sub Test()
    Dim ws1 As Worksheet
    Set ws1 = ThisWorkbook.Worksheets("Master")
    ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
    End Sub
    
    0 讨论(0)
  • 2020-12-01 11:26

    I really liked @brettdj's code, but then I found that when I added additional code to edit the copy, it overwrote my original sheet instead. I've tweaked his answer so that further code pointed at ws1 will affect the new sheet rather than the original.

    Sub Test()
        Dim ws1 as Worksheet
        ThisWorkbook.Worksheets("Master").Copy
        Set ws1 = ThisWorkbook.Worksheets("Master (2)")
    End Sub
    
    0 讨论(0)
提交回复
热议问题