2011-07-27 143 views
10

有誰知道如何使用Excel VBA檢查Excel文檔中是否存在某些圖紙?如何在Excel-VBA中檢查某些圖紙是否存在?

+0

可能重複http://stackoverflow.com/questions/6040164/excel- vba-if-worksheetwsname-exists) –

+2

[Test or check if sheet exists](https://stackoverflow.com/questions/6688131/test-or-check-if-sheet-exists) – puzzlepiece87

回答

13

雖然(不幸)這樣的方法不可用,我們可以創建自己的函數來檢查這個..

希望下面的代碼符合您的需求。

EDIT1:增加也delete語句...

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 Programming wannabe'代碼...

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 
+0

這也是一個優秀的解決方案 - 無需依賴錯誤進行檢查。我知道有更好的辦法,而且我在工作之前就用過這種方式,我只是不記得如何去做。 @Tiago:你會如何在表單(1)上使用它?只需傳入表單(1).Name? –

+0

@PaulR,是的,正好......'Sheets(1).name'可以做到這一點。請注意,您可能需要主動定義要使用的工作簿,因爲「工作表」引用隱含指向活動工作簿(有時不是預期的)。 –

+0

對,有時候我有時會咬我。對於較大的操作,我傾向於爲我的工作表(「SheetName」)對象使用With塊,並稱其爲好。當然,在確定Worksheet(「SheetName」)存在之後,這將是。 –

3

像這樣的事情讓你開始:

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()子。

希望這會有所幫助!

0

我改編了這段代碼,用於IBMScript(以前的Lotus Notes)使用的語言之一LotusScript,如下所示。

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 
0
On Error GoTo Line1 
If Sheets("BOX2").Index > 0 Then 
Else 
Line1: MsgBox ("BOX2 is missing") 
end if 

我這樣:)做到了

[EXCEL VBA如果工作表( 「wsName」)是否存在(的
相關問題