Copy sheet and get resulting sheet object?

后端 未结 13 2163
醉梦人生
醉梦人生 2020-11-28 12:21

Is there any easy/short way to get the worksheet object of the new sheet you get when you copy a worksheet?

ActiveWorkbook.Sheets(\         


        
13条回答
  •  鱼传尺愫
    2020-11-28 13:18

    Based on Trevor Norman's method, I've developed a function for copying a sheet and returning a reference to the new sheet.

    1. Unhide the last sheet (1) if not visible
    2. Copy the source sheet (2) after the last sheet (1)
    3. Set the reference to the new sheet (3), i.e. the sheet after the last sheet (1)
    4. Hide the last sheet (1) if necessary

    Code:

    Function CopySheet(ByRef sourceSheet As Worksheet, Optional ByRef destinationWorkbook As Workbook) As Worksheet
    
        Dim newSheet As Worksheet
        Dim lastSheet As Worksheet
        Dim lastIsVisible As XlSheetVisibility
    
        If destinationWorkbook Is Nothing Then Set destinationWorkbook = sourceSheet.Parent
    
        With destinationWorkbook
            Set lastSheet = .Worksheets(.Worksheets.Count)
        End With
    
        ' store visibility of last sheet
        lastIsVisible = lastSheet.Visible
        ' make the last sheet visible
        lastSheet.Visible = xlSheetVisible
    
        sourceSheet.Copy After:=lastSheet
        Set newSheet = lastSheet.Next
    
        ' restore visibility of last sheet
        lastSheet.Visible = lastIsVisible
    
        Set CopySheet = newSheet
    
    End Function
    

    This will always insert the copied sheet at the end of the destination workbook.

    After this, you can do any moves, renames, etc.

    Usage:

    Sub Sample()
    
        Dim newSheet As Worksheet
    
        Set newSheet = CopySheet(ThisWorkbook.Worksheets("Template"))
    
        Debug.Print newSheet.Name
    
        newSheet.Name = "Sample" ' rename new sheet
        newSheet.Move Before:=ThisWorkbook.Worksheets(1) ' move to beginning
    
        Debug.Print newSheet.Name
    
    End Sub
    

    Or if you want the behaviour/interface to be more similar to the built-in Copy method (i.e. before/after), you could use:

    Function CopySheetTo(ByRef sourceSheet As Worksheet, Optional ByRef beforeSheet As Worksheet, Optional ByRef afterSheet As Worksheet) As Worksheet
    
        Dim destinationWorkbook As Workbook
        Dim newSheet As Worksheet
        Dim lastSheet As Worksheet
        Dim lastIsVisible As XlSheetVisibility
    
        If Not beforeSheet Is Nothing Then
            Set destinationWorkbook = beforeSheet.Parent
        ElseIf Not afterSheet Is Nothing Then
            Set destinationWorkbook = afterSheet.Parent
        Else
            Set destinationWorkbook = sourceSheet.Parent
        End If
    
        With destinationWorkbook
            Set lastSheet = .Worksheets(.Worksheets.Count)
        End With
    
        ' store visibility of last sheet
        lastIsVisible = lastSheet.Visible
        ' make the last sheet visible
        lastSheet.Visible = xlSheetVisible
    
        sourceSheet.Copy After:=lastSheet
        Set newSheet = lastSheet.Next
    
        ' restore visibility of last sheet
        lastSheet.Visible = lastIsVisible
    
        If Not beforeSheet Is Nothing Then
            newSheet.Move Before:=beforeSheet
        ElseIf Not afterSheet Is Nothing Then
            newSheet.Move After:=afterSheet
        Else
            newSheet.Move After:=sourceSheet
        End If
    
        Set CopySheetTo = newSheet
    
    End Function
    

提交回复
热议问题