testing或检查是否存在表单

Dim wkbkdestination As Workbook Dim destsheet As Worksheet For Each ThisWorkSheet In wkbkorigin.Worksheets 'this throws subscript out of range if there is not a sheet in the destination 'workbook that has the same name as the current sheet in the origin workbook. Set destsheet = wkbkdestination.Worksheets(ThisWorkSheet.Name) Next 

基本上,我遍历原始工作簿中的所有工作表,然后将目标工作簿中的目标工作表设置为与原工作簿中当前迭代工作簿具有相同名称的工作表。

我如何testing该表是否存在? 就像是:

 If wkbkdestination.Worksheets(ThisWorkSheet.Name) Then 

一些民间人士不喜欢这种方法,因为error handling的“不恰当”使用,但我认为它在VBA中被认为是可以接受的。另一种方法是循环所有的表格,直到find匹配。

  Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean Dim sht As Worksheet If wb Is Nothing Then Set wb = ThisWorkbook On Error Resume Next Set sht = wb.Sheets(shtName) On Error GoTo 0 SheetExists = Not sht Is Nothing End Function 

如果您仅对工作表感兴趣,则可以使用简单的Evaluate调用:

 Function WorksheetExists(sName As String) As Boolean WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)") End Function 

您不需要error handling为了完成此。 所有你需要做的是遍历所有的工作表,并检查指定的名称是否存在:

 For i = 1 To Worksheets.Count If Worksheets(i).Name = "MySheet" Then exists = True End If Next i If Not exists Then Worksheets.Add.Name = "MySheet" End If 

检查一个集合的成员是一个普遍的问题,这里是一个抽象的Tim的答案:

函数包含(objCollection As Object,strName as String)作为布尔值
    昏暗的o作为对象
    在错误恢复下一步
     set o = objCollection(strName)
    包含=(Err.Number = 0)
     Err.Clear
 结束function

这个函数可以用于像对象( ShapesRangeNamesWorkbooks等)的任何集合。

要检查是否存在图纸,请使用If Contains(Sheets, "SheetName") ...

更正:没有error handling:

 Function CheckIfSheetExists(SheetName As String) As Boolean CheckIfSheetExists = False For Each WS In Worksheets If SheetName = WS.name Then CheckIfSheetExists = True Exit Function End If Next WS End Function 

如果有人想避免使用VBA,并testing工作表是否纯粹存在于单元格公式中,则可以使用ISREFINDIRECT函数:

=ISREF(INDIRECT("SheetName!A1"))

如果工作簿包含名为SheetName的工作表,则返回TRUE否则返回FALSE

我的解决scheme看起来非常像Tims,但在非工作表的图表中也可以工作

 Public Function SheetExists(strSheetName As String, Optional wbWorkbook As Workbook) As Boolean    If wbWorkbook Is Nothing Then Set wbWorkbook = ActiveWorkbook 'or ThisWorkbook - whichever appropriate    Dim obj As Object    On Error GoTo HandleError    Set obj = wbWorkbook.Sheets(strSheetName)    SheetExists = True    Exit Function HandleError:    SheetExists = False End Function 

把testing放在一个函数中,你将能够重用它,你有更好的代码可读性。

不要使用“On Error Resume Next”,因为它可能与您的代码的其他部分冲突。

 Sub DoesTheSheetExists() If SheetExist("SheetName") Then Debug.Print "The Sheet Exists" Else Debug.Print "The Sheet Does NOT Exists" End If End Sub Function SheetExist(strSheetName As String) As Boolean Dim i As Integer For i = 1 To Worksheets.Count If Worksheets(i).Name = strSheetName Then SheetExist = True Exit Function End If Next i End Function 

很多年后,但我只是需要这样做,不喜欢任何贴出的解决scheme…所以我做了一个,都感谢(海绵宝宝手势)“评估()”的魔术!

 Evaluate("IsError(" & vSheetName & "!1:1)") 

如果Sheet不存在,则返回TRUE; 如果表单存在,则返回FALSE。 你可以用“1:1”replace你想要的范围,但是我build议不要使用单个单元格,如果它包含错误(例如#N / A),则返回True。

如果你是WorksheetFunction.的粉丝WorksheetFunction. 或者你从一个非英语国家的非英语Excel工作这是一个很好的解决scheme,这工作:

 WorksheetFunction.IsErr(Evaluate("'" & wsName & "'!A1")) 

或者像这样的function:

 Function WorksheetExists(sName As String) As Boolean WorksheetExists = Not WorksheetFunction.IsErr(Evaluate("'" & sName & "'!A1")) End Function 
 Public Function WorkSheetExists(ByVal strName As String) As Boolean On Error Resume Next WorkSheetExists = Not Worksheets(strName) Is Nothing End Function sub test_sheet() If Not WorkSheetExists("SheetName") Then MsgBox "Not available" Else MsgBox "Available" End If End Sub 

为什么不使用小循环来确定命名工作表是否存在? 假设您正在查找当前打开的工作簿中名为“Sheet1”的工作表。

 Dim wb as Workbook Dim ws as Worksheet Set wb = ActiveWorkbook For Each ws in wb.Worksheets if ws.Name = "Sheet1" then 'Do something here End if Next 

我做了另一件事情:只有在存在的情况下才删除表单 – 如果不存在,则不会发生错误:

 Excel.DisplayAlerts = False Dim WS For Each WS In Excel.Worksheets If WS.name = "Sheet2" Then Excel.sheets("Sheet2").Delete Exit For End If Next Excel.DisplayAlerts = True 
  For Each Sheet In Worksheets If UCase(Sheet.Name) = "TEMP" Then 'Your Code when the match is True Application.DisplayAlerts = False Sheet.Delete Application.DisplayAlerts = True '----------------------------------- End If Next Sheet 

将“数据”更改为您正在testing的任何表单名称…

 On Error Resume Next Set DataSheet = Sheets("Data") If DataSheet Is Nothing Then Sheets.Add(after:=ActiveSheet).Name = "Data" ''or whatever alternate code you want to execute'' End If On Error GoTo 0 

毫无疑问,上面的函数可以工作,我只是结束了以下代码工作得很好:

 Sub Sheet_exist () On Error Resume Next If Sheets("" & Range("Sheet_Name") & "") Is Nothing Then MsgBox "doesnt exist" Else MsgBox "exist" End if End sub 

注意: Sheets_Name是我要求用户input名称的地方,所以这可能与您不一样。

我实际上有一个简单的方法来检查工作表是否存在,然后执行一些指令:

在我的情况下,我想删除工作表,然后重新创build相同的名单,但代码被中断,如果程序无法删除工作表,因为它已被删除

 Sub Foo () Application.DisplayAlerts = False On Error GoTo instructions Sheets("NAME OF THE SHEET").Delete instructions: Sheets.Add After:=Sheets(Sheets.Count) ActiveSheet.Name = "NAME OF THE SHEET" End Sub