名称使用VBA新创建的选项卡已存在错误

时间:2017-12-11 19:35:51

标签: excel vba excel-vba

我尝试在excel中创建VBA宏,其中一个Excel工作表跟踪路径并在另一个工作表中创建新选项卡。它工作得很好,但是当我“意外地”使用相同名称创建另一个标签时,它会给出错误,因为“名称已经尝试了另一个”。我不想再创建一个具有相同名称的选项卡。相反,它应该阻止我创建具有相同名称的选项卡

无论如何,如果该名称已经存在,它会弹出一个名称已经存在的名称我只有一个选项可以点击。我单击“确定”,并且创建的附加工作表不会被保存(或者如果已经创建,则会自行删除或保存为与(2)旁边的相同名称,因为excel通常会对重复工作表执行此操作)。我正在尝试这样的事情

If wb.ActiveSheet.Name = sName Then wb.ActiveSheet.Delete

这是我的代码

Private Sub Filling_List()

Dim sPath As String
Dim sFile As String
Dim wb As Workbook

Dim sName As String 'add sName declaration

Dim wb1 As Workbook
Dim ws1 As Worksheet
Set wb1 = ThisWorkbook
Set ws1 = ThisWorkbook.Worksheets("S0")

Application.ScreenUpdating = False

sPath = "C:\Users\arp\Desktop\Filling list macro\"
sFile = sPath & "ArF Filling List.xlsm"

Set wb = Workbooks.Open(sFile)

wb.Worksheets("ArF Templete").Copy After:=Worksheets(Worksheets.Count)

sName = ws1.Range("A1") & " " & ws1.Range("T2")  

wb.ActiveSheet.Name = sName

'如果wb.ActiveSheet.Name = sName那么wb.ActiveSheet.Delete“我正在尝试这个但它不起作用”

If sName = vbNullString Then Exit Sub 'compare against vbNullstring not empty string literal

With wb.Worksheets(sName)

.Cells(3, "E") = InputBox("Your Initials:")
'.Cells(5, "E") = InputBox("Col?:")
.Cells(6, "E") = InputBox("I:")
.Cells(7, "E") = InputBox("ET1 B:")
.Range("B03") = wb1.Worksheets("Que").Range("B02").Value2
.Range("B04") = wb1.Worksheets("Que").Range("E01").Value2
.Range("B05") = wb1.Worksheets("Que").Range("B01").Value2
.Cells(3, "E") = wb1.Worksheets("Que").Range("E02").Value2
.Cells(5, "E") = "Yes"
'Filling order
.Range("B38:B43") = wb1.Worksheets("Que & Tsc Cal").Range("B04:B09").Value2
.Range("C38:C43") = wb1.Worksheets("Que & Tsc Cal").Range("C04:C09").Value2
.Range("D38:D43") = wb1.Worksheets("Que & Tsc Cal").Range("A04:A09").Value2

'Retains

End With


Application.ScreenUpdating = True

End Sub

我在这里的帮助下开发了上面的版本,并加入了其他线程中的点点滴滴。我们非常欢迎任何更好的建议。

3 个答案:

答案 0 :(得分:5)

我使用检查指定的标签/表格是否可用:

If IsError(Evaluate("SHEETNAME!A1")) Then
    'Nothing
Else
    Sheets("SHEETNAME").Delete
End If
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "SHEETNAME"

或者斯科特建议让它更简单,更清洁:

If Not IsError(Evaluate("SHEETNAME!A1")) Then Sheets("SHEETNAME").Delete
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "SHEETNAME"

编辑1:

Application.DisplayAlerts = False
If IsError(Evaluate("SHEETNAME!A1")) Then Sheets.Add(After:=Sheets(Sheets.Count)).Name = "SHEETNAME"
Application.DisplayAlerts = True

答案 1 :(得分:1)

下面的代码应该做你想要的,你可能需要根据你的项目进行调整。

Option Explicit

Sub addsheet()
    Dim sht As Worksheet
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets.add

    On Error Resume Next                                    'Prevent Excel from stopping on an error but just goes to next line
    ws.Name = "Sheet1"

    If Err.Number = 1004 Then
        MsgBox "Worksheet with this name already exists"
        Application.DisplayAlerts = False                   'Prevent confirmation popup on sheet deletion
        ws.Delete
        Application.DisplayAlerts = True                    'Turn alerts back on
        On Error GoTo 0                                     'Stop excel from skipping errors
        Exit Sub                                            'Terminate sub after a failed attempt to add sheet
    End If

    On Error GoTo 0                                         'Stop Excel from skipping errors.

End Sub

答案 2 :(得分:1)

  

[W]如果我使用相同的名称“意外地”创建了另一个标签,它会给我错误。 。 。我不想再创建一个具有相同名称的选项卡。相反,它应该阻止我创建具有相同名称的选项卡

创建选项卡的宏不是一个不常见的问题 - 很容易意外地运行它们两次。要防止出现这种情况,请首先检查选项卡是否已存在,并且只有在验证它不存在后,才能调用Worksheets.Copy方法。

Private Sub Filling_List()

Dim sPath As String
Dim sFile As String
Dim wb As Workbook

Dim sName As String 'add sName declaration

Dim wb1 As Workbook
Dim ws1 As Worksheet
Set wb1 = ThisWorkbook
Set ws1 = ThisWorkbook.Worksheets("S0")

Application.ScreenUpdating = False

sPath = "C:\Users\arp\Desktop\Filling list macro\"
sFile = sPath & "ArF Filling List.xlsm"

Set wb = Workbooks.Open(sFile)

sName = ws1.Range("A1") & " " & ws1.Range("T2")

On Error Resume Next
Dim wslTest As Worksheet
Set wslTest = wb.Worksheets(sName)
If Err.Number = 0 Then
    MsgBox "Tab: " & sName & " already exists.", vbInformation
    wslTest.Activate
    Exit Sub
End If
On Error GoTo 0

wb.Worksheets("ArF Templete").Copy After:=wb.Worksheets(wb.Worksheets.Count)
wb.ActiveSheet.Name = sName


' rest of code


End Sub