有誰知道如何使用Excel VBA檢查Excel文檔中是否存在某些圖紙?如何在Excel-VBA中檢查某些圖紙是否存在?
回答
雖然(不幸)這樣的方法不可用,我們可以創建自己的函數來檢查這個..
希望下面的代碼符合您的需求。
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
這也是一個優秀的解決方案 - 無需依賴錯誤進行檢查。我知道有更好的辦法,而且我在工作之前就用過這種方式,我只是不記得如何去做。 @Tiago:你會如何在表單(1)上使用它?只需傳入表單(1).Name? –
@PaulR,是的,正好......'Sheets(1).name'可以做到這一點。請注意,您可能需要主動定義要使用的工作簿,因爲「工作表」引用隱含指向活動工作簿(有時不是預期的)。 –
對,有時候我有時會咬我。對於較大的操作,我傾向於爲我的工作表(「SheetName」)對象使用With塊,並稱其爲好。當然,在確定Worksheet(「SheetName」)存在之後,這將是。 –
像這樣的事情讓你開始:
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()子。
希望這會有所幫助!
我改編了這段代碼,用於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
On Error GoTo Line1
If Sheets("BOX2").Index > 0 Then
Else
Line1: MsgBox ("BOX2 is missing")
end if
我這樣:)做到了
[EXCEL VBA如果工作表( 「wsName」)是否存在(的- 1. Bash:如何檢查某些文件是否存在?
- 2. 檢查是否存在,是否存在,是否爲某個值
- 3. 檢查SQLite中是否存在某些內容
- 4. 檢查集合中是否存在某些參數
- 5. 如何檢查某些數字是否出現在數組中?
- 6. 如何檢查某些字符是否在字符串中?
- 7. 如何檢查在php中是否回顯了某些內容?
- 8. 如何檢查視圖是否存在?
- 9. 如何檢查視圖是否存在?
- 10. 如何檢查PHP中是否存在某些類似格式的文件?
- 11. XSLT - 檢查是否存在某些元素或節點
- 12. 檢查div後是否存在某些東西
- 13. 檢查圖像是否存在使用url在某些情況下返回false
- 14. 如何檢查java中是否存在某個.txt文件
- 15. 如何檢查列表框中是否存在某個值?
- 16. 如何檢查當前循環中是否存在某個值?
- 17. 如何檢查某些意圖是否已啓動
- 18. 如何檢查圖像視圖中是否存在圖像?
- 19. 使用Selenium2,我如何檢查頁面上是否存在某些文本?
- 20. 檢查圖像中是否存在Java
- 21. 如何檢查子視圖是否存在於UIScrollView的某個框架中?
- 22. 如何檢查特定圖案是否在某些字符之前?
- 23. 如何檢查某個組件是否存在?
- 24. 如何檢查某個特定日期是否存在?
- 25. 如何檢查JDBC表中的某些列是否爲Java
- 26. 如何檢查用戶是否有某些表中的數據
- 27. 在Django中,如何檢查用戶是否在某個組中?
- 28. 檢測InstallScript中是否存在某些IIS功能
- 29. 檢查是否存在某個URL並返回圖像
- 30. 檢查某些信息是否已經在數據庫中
可能重複http://stackoverflow.com/questions/6040164/excel- vba-if-worksheetwsname-exists) –
[Test or check if sheet exists](https://stackoverflow.com/questions/6688131/test-or-check-if-sheet-exists) – puzzlepiece87