如何在所有Excel工作表的末尾添加命名工作表?


72

我试图在所有现有工作表的末尾添加一个名为“ Temp”的Excel工作表,但是此代码不起作用:

Private Sub CreateSheet()
    Dim ws As Worksheet
    ws.Name = "Tempo"
    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
End Sub

你能告诉我为什么吗?


2
因为您尚未在ws分配之前创建.Name。请勿混淆:在尝试访问其属性之前,先实例化该变量。
NullDev 2013年

Answers:


156

尝试这个:

Private Sub CreateSheet()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.Name = "Tempo"
End Sub

或使用With子句避免重复调出对象

Private Sub CreateSheet()
    Dim ws As Worksheet
    With ThisWorkbook
        Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        ws.Name = "Tempo"
    End With
End Sub

如果您不需要在其余的代码中调用同一工作表,则可以进一步简化上述内容。

Sub CreateSheet()
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Temp"
    End With
End Sub

为什么出什么事了?您不是在同一工作簿中创建工作表?
L42

11
ThisWorkbook是承载正在运行的宏的工作簿。ActiveWorkbook是用户正在工作的工作簿(用简单的术语表示)。WorkBooks(“ somename”)解决了一个特定的打开工作簿
Massimo Fuccillo 2013年

1
很好,但是如果工作表已经存在会发生什么,会抛出错误吗?
Ch32k0

1
@ Ch32k0是的。如果已经存在相同的名称,或者您使用的名称无效。
2016年

@ L42哦,我对此进行了尝试,实际上确实如此,感谢您的反馈
Ch32k0 2016年

37

请使用这一衬里:

Sheets.Add(After:=Sheets(Sheets.Count)).Name = "new_sheet_name"

1
抱歉,接受的答案看起来更好。这个没有明确指出ThisWorkbookActiveWorkbook。同样,这里的新工作表对象未分配给变量,而之后几乎总是要使用它来将数据放入表中。
ZygD

我认为这个答案最有效。未指定应将工作表分配给变量。他显然知道它的名称,因为它是在代码中指定的。
IronWilliamCash

7
ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "XYZ"

(添加工作表时,无论如何它将是活动表)


5

尝试这个:

Public Enum iSide
iBefore
iAfter
End Enum
Private Function addSheet(ByRef inWB As Workbook, ByVal inBeforeOrAfter As iSide, ByRef inNamePrefix As String, ByVal inName As String) As Worksheet
    On Error GoTo the_dark

    Dim wsSheet As Worksheet
    Dim bFoundWS As Boolean
    bFoundWS = False
    If inNamePrefix <> "" Then
        Set wsSheet = findWS(inWB, inNamePrefix, bFoundWS)
    End If

    If inBeforeOrAfter = iAfter Then
        If wsSheet Is Nothing Or bFoundWS = False Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = inName
        Else
            Worksheets.Add(After:=wsSheet).Name = inName
        End If
    Else
        If wsSheet Is Nothing Or bFoundWS = False Then
            Worksheets.Add(Before:=Worksheets(1)).Name = inName
        Else
            Worksheets.Add(Before:=wsSheet).Name = inName
        End If
    End If

    Set addSheet = findWS(inWB, inName, bFoundWS)         ' just to confirm it exists and gets it handle

    the_light:
    Exit Function
    the_dark:
    MsgBox "addSheet: " & inName & ": " & Err.Description, vbOKOnly, "unexpected error"
    Err.Clear
    GoTo the_light
End Function

4

尝试使用:

Worksheets.Add (After:=Worksheets(Worksheets.Count)).Name = "MySheet"

如果要检查是否存在具有相同名称的图纸,则可以创建一个函数:

Function funcCreateList(argCreateList)
    For Each Worksheet In ThisWorkbook.Worksheets
        If argCreateList = Worksheet.Name Then
            Exit Function ' if found - exit function
        End If
    Next Worksheet
    Worksheets.Add (After:=Worksheets(Worksheets.Count)).Name = argCreateList
End Function

创建函数后,可以从主Sub调用它,例如:

Sub main

    funcCreateList "MySheet"

Exit Sub

2

这将为您提供以下选择:

  1. 覆盖或保留具有相同名称的选项卡。
  2. 将工作表放在所有选项卡的末尾或当前选项卡的旁边。
  3. 选择您的新表或活动表。

Call CreateWorksheet("New", False, False, False)


Sub CreateWorksheet(sheetName, preserveOldSheet, isLastSheet, selectActiveSheet)
  activeSheetNumber = Sheets(ActiveSheet.Name).Index

  If (Evaluate("ISREF('" & sheetName & "'!A1)")) Then 'Does sheet exist?
    If (preserveOldSheet) Then
      MsgBox ("Can not create sheet " + sheetName + ". This sheet exist.")
      Exit Sub
    End If
      Application.DisplayAlerts = False
      Worksheets(sheetName).Delete
    End If

    If (isLastSheet) Then
      Sheets.Add(After:=Sheets(Sheets.Count)).Name = sheetName 'Place sheet at the end.
    Else 'Place sheet after the active sheet.
      Sheets.Add(After:=Sheets(activeSheetNumber)).Name = sheetName
    End If

    If (selectActiveSheet) Then
      Sheets(activeSheetNumber).Activate
    End If

End Sub

1


尝试切换代码顺序。您必须先创建工作表才能命名。

Private Sub CreateSheet()
    Dim ws As Worksheet
    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
    ws.Name = "Tempo"
End Sub

谢谢,


-3

这是将命名选项卡快速简单地添加到当前工作表中:

Sheets.Add.Name = "Tempo"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.