复制和重命名工作表

时间:2015-01-07 22:17:50

标签: excel vba excel-vba

有人可以帮我解决一下如何根据另一张纸上的值复制和重命名纸张, 目前这张纸被称为" CLO(2)"不是" CLO 1.2"。我希望代码能够复制名为" CLO"并根据名为" TMP Data"的工作表上的值重命名,使用B7和C7及以下的单元格值(B和C值创建工作表名称)。

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Count > 1 Then Exit Sub

    If Target.Address = "$B$6" Then

        Application.EnableEvents = False

        With Target.Offset(1, 0)

            Range(.Cells(1), .Cells(1).End(xlDown)).ClearContents

            .Value = 1

            .Resize(Target.Value, 1).DataSeries _

              Rowcol:=xlColumns, Type:=xlLinear, Step:=1

        End With

        Target.Offset(0, 1).Activate

    ElseIf Not Intersect(Target, Columns("C")) Is Nothing Then

        If Target.Row > 6 And Application.Count(Target.Offset(0, -1).Resize(1, 2)) = 2 Then

            On Error GoTo Fìn

            Application.EnableEvents = False

            Dim w As Long

            For w = 1 To Target.Value

                Sheets("CLO").Copy(after:=Sheets(Sheets.Count)).Name = _

                  Target.Offset(0, -1) & Chr(46) & w

            Next w

            Me.Activate

        End If

    End If
Fìn:

    Application.EnableEvents = True

End Sub

Here is the Excel file

1 个答案:

答案 0 :(得分:0)

默认情况下,工作表将被复制为CLO(2),复制后重命名:

Sheets("CLO").Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = Target.Offset(0, -1).Value & Chr(46) & w