31

我想复制一张工作表并将其添加到所有当前工作表的末尾(无论工作表是否隐藏)。

Sheets(1).Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).name = "copied sheet!"

这工作正常,除了当有隐藏工作表时,新工作表仅插入最后一个可见工作表之后,因此该name命令重命名错误工作表。

我尝试了以下变体来获取对新复制的引用,WorkSheet但没有一个是成功和/或有效的代码。

Dim test As Worksheet
Set test = Sheets(1).Copy(After:=Sheets(Sheets.Count))
test.Name = "copied sheet!"
4

7 回答 7

43

Try this

Sub Sample()
    Dim test As Worksheet
    Sheets(1).Copy After:=Sheets(Sheets.Count)
    Set test = ActiveSheet
    test.Name = "copied sheet!"
End Sub

Looking back at this, a better approach would be

Set test = Sheets(Sheets.Count)

As correctly mentioned in the comments below, there are lot of things that needs to be considered when copying and renaming a sheet. Would recommend checking the other answers as well.

于 2012-08-16T15:23:15.700 回答
9

Make the source sheet visible before copying. Then copy the sheet so that the copy also stays visible. The copy will then be the active sheet. If you want, hide the source sheet again.

于 2014-08-14T09:17:04.353 回答
2

在将工作表复制到另一个工作簿时,我遇到了类似的问题。我更喜欢避免使用“activesheet”,因为它在过去给我带来了问题。因此,我编写了一个函数来根据我的需要执行此操作。我在这里为那些像我一样通过谷歌到达的人添加它:

这里的主要问题是将可见工作表复制到最后一个索引位置会导致 Excel 将工作表重新定位到可见工作表的末尾。因此,将工作表复制到最后一张可见工作表之后的位置可以解决此问题。即使您正在复制隐藏的工作表。

Function Copy_WS_to_NewWB(WB As Workbook, WS As Worksheet) As Worksheet
    'Creates a copy of the specified worksheet in the specified workbook
    '   Accomodates the fact that there may be hidden sheets in the workbook
    
    Dim WSInd As Integer: WSInd = 1
    Dim CWS As Worksheet
    
    'Determine the index of the last visible worksheet
    For Each CWS In WB.Worksheets
        If CWS.Visible Then If CWS.Index > WSInd Then WSInd = CWS.Index
    Next CWS
    
    WS.Copy after:=WB.Worksheets(WSInd)
    Set Copy_WS_to_NewWB = WB.Worksheets(WSInd + 1)

End Function

要将此功能用于原始问题(即在同一工作簿中),可以使用类似...

Set test = Copy_WS_to_NewWB(Workbooks(1), Workbooks(1).Worksheets(1))
test.name = "test sheet name"

EDIT 04/11/2020 from –user3598756 添加对上述代码的轻微重构

Function CopySheetToWorkBook(targetWb As Workbook, shToBeCopied As Worksheet, copiedSh As Worksheet) As Boolean
    'Creates a copy of the specified worksheet in the specified workbook
    '   Accomodates the fact that there may be hidden sheets in the workbook

    Dim lastVisibleShIndex As Long
    Dim iSh As Long

    On Error GoTo SafeExit
    
    With targetWb
        'Determine the index of the last visible worksheet
        For iSh = .Sheets.Count To 1 Step -1
            If .Sheets(iSh).Visible Then
                lastVisibleShIndex = iSh
                Exit For
            End If
        Next
    
        shToBeCopied.Copy after:=.Sheets(lastVisibleShIndex)
        Set copiedSh = .Sheets(lastVisibleShIndex + 1)
    End With
    
    CopySheetToWorkBook = True
    Exit Function
    
SafeExit:
    
End Function

除了使用不同的(更具描述性的?)变量名之外,重构主要处理:

  1. 将函数类型转换为布尔值,同时在函数参数中包含返回的(复制的)工作表列出这个,让调用子处理可能的错误,比如

     Dim WB as Workbook: Set WB = ThisWorkbook ' as an example
     Dim sh as Worksheet: Set sh = ActiveSheet ' as an example
     Dim copiedSh as Worksheet
     If CopySheetToWorkBook(WB, sh, copiedSh) Then
         ' go on with your copiedSh sheet
     Else
         Msgbox "Error while trying to copy '" & sh.Name & "'" & vbcrlf & err.Description
     End If
    
  2. 让 For - Next 循环从最后一个工作表索引向后步进并在第一个可见工作表出现时退出,因为我们在“最后一个”可见工作表之后

于 2016-12-07T20:01:03.737 回答
0

如果您使用基于@Siddharth Rout 的代码的以下代码,则重命名刚刚复制的工作表,无论它是否被激活。

Sub Sample()

    ThisWorkbook.Sheets(1).Copy After:=Sheets(Sheets.Count)
    ThisWorkbook.Sheets(Sheets.Count).Name = "copied sheet!"

End Sub
于 2015-04-24T10:47:42.223 回答
0

当您要复制名为“mySheet”的工作表并使用 .Copy After:= 时,Excel 首先将复制的工作表命名为完全相同,并简单地添加“(2)”,使其最终名称为“mySheet (2)”。

隐藏与否,无所谓。它用 2 行代码震撼,在工作簿的末尾添加复制的工作表!!!

例子:

Sheets("mySheet").Copy After:=Sheets(ThisWorkbook.Sheets.count)
Sheets("mySheet (2)").name = "TheNameYouWant"

简单不!

于 2019-05-22T14:37:17.207 回答
0

将此代码添加到开头:

    Application.ScreenUpdating = False
     With ThisWorkbook
      Dim ws As Worksheet
       For Each ws In Worksheets: ws.Visible = True: Next ws
     End With

将此代码添加到末尾:

    With ThisWorkbook
     Dim ws As Worksheet
      For Each ws In Worksheets: ws.Visible = False: Next ws
    End With
     Application.ScreenUpdating = True

如果您希望超过第一张工作表处于活动状态且可见,请在最后调整代码。如下所示:

     Dim ws As Worksheet
      For Each ws In Worksheets
       If ws.Name = "_DataRecords" Then

         Else: ws.Visible = False
       End If
      Next ws

为确保新工作表是重命名的工作表,请调整您的代码,如下所示:

     Sheets(Me.cmbxSheetCopy.value).Copy After:=Sheets(Sheets.Count)
     Sheets(Me.cmbxSheetCopy.value & " (2)").Select
     Sheets(Me.cmbxSheetCopy.value & " (2)").Name = txtbxNewSheetName.value

此代码来自我的用户表单,它允许我将具有我想要的格式和公式的特定工作表(从下拉框中选择)复制到新工作表,然后使用用户输入重命名新工作表。请注意,每次复制工作表时,都会自动为其指定旧工作表名称,并指定“(2)”。示例“OldSheet”在复制之后和重命名之前变为“OldSheet (2)”。因此,您必须在重命名之前选择带有程序命名的已复制工作表。

于 2017-06-22T20:34:29.033 回答
-3

答:我找到了这个,想和你分享。

Sub Copier4()
   Dim x As Integer

   For x = 1 To ActiveWorkbook.Sheets.Count
      'Loop through each of the sheets in the workbook
      'by using x as the sheet index number.
      ActiveWorkbook.Sheets(x).Copy _
         After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
         'Puts all copies after the last existing sheet.
   Next
End Sub

但是问题是,我们可以使用它和以下代码来重命名工作表吗?如果可以,我们该怎么做?

Sub CreateSheetsFromAList()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("Summary").Range("A10")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
Next MyCell
End Sub
于 2015-01-16T06:17:02.860 回答