Copy sheet and get resulting sheet object?

后端 未结 13 2130
醉梦人生
醉梦人生 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:12

    I realise this post is over a year old, but I came here looking for an answer to the same issue regarding copying sheets and unexpected results caused by hidden sheets. None of the above really suited what I wanted mainly because of the structure of my workbook. Essentailly it has a very large number of sheets and what is displayed is driven by a user selecting the specific functionality, plus the order of the visible sheets was importnat to me so i didnt want to mess with those. So my end solution was to rely on Excels default naming convention for copied sheets, and explictly rename the new sheet by name. Code sample below (as an aside, my workbook has 42 sheets and only 7 are permanently visible, and the after:=Sheets(Sheets.count) put my copied sheet in the middle of the 42 sheets, depending on what sheets are visible at the time.

            Select Case DCSType
            Case "Radiology"
                'Copy the appropriate Template to a new sheet at the end
                TemplateRAD.Copy after:=Sheets(Sheets.count)
                wsToCopyName = TemplateRAD.Name & " (2)"
                'rename it as "Template"
                Sheets(wsToCopyName).Name = "Template"
                'Copy the appropriate val_Request to a new sheet at the end
                valRequestRad.Copy after:=Sheets(Sheets.count)
                'rename it as "val_Request"
                wsToCopyName = valRequestRad.Name & " (2)"
                Sheets(wsToCopyName).Name = "val_Request"
            Case "Pathology"
                'Copy the appropriate Template to a new sheet at the end
                TemplatePath.Copy after:=Sheets(Sheets.count)
                wsToCopyName = TemplatePath.Name & " (2)"
                'rename it as "Template"
                Sheets(wsToCopyName).Name = "Template"
                'Copy the appropriate val_Request to a new sheet at the end
                valRequestPath.Copy after:=Sheets(Sheets.count)
                wsToCopyName = valRequestPath.Name & " (2)"
                'rename it as "val_Request"
                Sheets(wsToCopyName).Name = "val_Request"
        End Select
    

    Anyway, posted just in case its useful to anyone else

提交回复
热议问题