【问题标题】:How to check whether certain sheets exist or not in Excel-VBA? [duplicate]如何检查 Excel-VBA 中是否存在某些工作表? [复制]
【发布时间】:2011-10-13 21:13:57
【问题描述】:

有谁知道如何使用 Excel VBA 检查 Excel 文档中是否存在某些工作表?

【问题讨论】:

    标签: excel vba excel-2007


    【解决方案1】:

    虽然(不幸的是)这种方法不可用,但我们可以创建自己的函数来检查这个..

    希望下面的代码能满足您的需求。

    Edit1:还添加了删除语句...

    Sub test()
    
        If CheckSheet(Sheets(3).Name) then
    
            Application.DisplayAlerts = False
            Sheets(Sheets(3).Name).Delete
            Application.DisplayAlerts = True
    
        End If
    
    End Sub
    

    我想要的解决方案...

    Function CheckSheet(ByVal sSheetName As String) As Boolean
    
        Dim oSheet As Excel.Worksheet
        Dim bReturn As Boolean
    
        For Each oSheet In ActiveWorkbook.Sheets
    
            If oSheet.Name = sSheetName Then
    
                bReturn = True
                Exit For
    
            End If
    
        Next oSheet
    
        CheckSheet = bReturn
    
    End Function
    

    或者,如果您不介意使用会主动引发错误的代码(常见的编码最佳实践不建议这样做),您可以使用下面的“Spartan Programmingwannabe”代码...

    Function CheckSheet(ByVal sSheetName As String) As Boolean
    
        Dim oSheet As Excel.Worksheet
        Dim bReturn As Boolean
    
        For Each oSheet In ActiveWorkbook.Sheets
    
            If oSheet.Name = sSheetName Then
    
                bReturn = True
                Exit For
    
            End If
    
        Next oSheet
    
        CheckSheet = bReturn
    
    End Function
    
    
    Function CheckSheet(ByVal sSheetName As String) As Boolean
    
        On Error Resume Next
        Dim oSheet As Excel.Worksheet
    
        Set oSheet = ActiveWorkbook.Sheets(sSheetName)
        CheckSheet = IIf(oSheet Is Nothing, False, True)
    
    End Function
    

    【讨论】:

    • 这也是一个很好的解决方案——不依赖错误进行检查。我知道有更好的方法,而且我以前在工作中使用过这种方法,我只是不记得该怎么做了。 @Tiago:您将如何在 Sheets(1) 上使用它?只需传入 Sheets(1).Name?
    • @PaulR,是的,确切地说...Sheets(1).name 可以解决问题。请注意,您可能需要主动定义要使用的工作簿,因为“工作表”引用隐式指向活动工作簿(有时这不是预期的)。
    • 对,我有时在工作中被咬到了。对于较大的操作,我倾向于为我的 Worksheets("SheetName") 对象使用 With 块,并称之为好。当然,这是在我确定 Worksheets("SheetName") 存在之后。
    • 嗨@Tiago Cardoso 感谢您的回答。但抱歉,如果我在这里问业余问题。我对 excel-vba 比较陌生,但是根据我的上下文,我检查了这些表(2)和(3)是否存在,我想初始化一个删除函数,但现在使用上面的代码。这个功能应该怎么实现呢?
    • 嗨@PaulR 我已经更新了上面的代码,因为我在实现 Tiago Cardoso 代码时遇到了一些问题。你能看看这里给我一些关于如何解决它的建议吗?谢谢
    【解决方案2】:

    这样的事情会让你开始:

    On Error Resume Next
    
    Dim wSheet as Worksheet
    Set wSheet = Sheets(1) ' can also be a string, such as Sheets("Sheet1")
    
    If wSheet Is Nothing Then
        MsgBox "Worksheet not found!"
        Set wSheet = Nothing ' make the worksheet point to nothing. 
        On Error GoTo 0 
    Else 
        MsgBox "Worksheet found!"
        Set wSheet = Nothing ' set the found Worksheet object to nothing.  You can use the found wSheet for your purposes, though.  
    End If
    

    此代码基于http://www.ozgrid.com/VBA/IsWorkbookOpen.htm。寻找 DoesSheetExist() 子。

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我修改了此代码以在 LotusScript 中使用,LotusScript 是 IBM Notes(以前称为 Lotus Notes)使用的语言之一,如下所示。

      Public Function ExcelSheetExists( _
          xlBook As Variant, _ ' Excel workbook object
          ByVal strSheetName As String _
          ) As Boolean
      
          On Error GoTo errHandler
      
          ForAll xlSheet In xlBook.Sheets
              If xlSheet.Name = strSheetName Then
                  ExcelSheetExists = True
                  Exit Forall
              End If
          End ForAll
      
          GoTo Done
      
      errHandler:
          ' Call MyCustomErrorHandler()
          Resume Done
      Done:
      
      End Function
      

      【讨论】:

        【解决方案4】:
        On Error GoTo Line1
        If Sheets("BOX2").Index > 0 Then
        Else
        Line1: MsgBox ("BOX2 is missing")
        end if 
        

        我是这样做的:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 2016-08-21
          相关资源
          最近更新 更多