vba 如果 WorkSheet("wsName") 存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6040164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If WorkSheet("wsName") Exists
提问by Matt Rowles
I'm wondering if there is clean cut functionality that returns True or False if a worksheet inside a workbook exists?
我想知道如果工作簿中存在工作表,是否有干净的剪切功能返回 True 或 False?
It would be good, but not essential, if it's possible to do it without skipping error handling.
如果可以在不跳过错误处理的情况下做到这一点,那会很好,但不是必需的。
The only thing I've found doesn't really work:
我发现的唯一一件事并没有真正起作用:
On Error Resume Next
If (Worksheets("wsName").Name <> "") Then
Debug.Print "Worksheet exists!"
Else
Debug.Print "Worksheet doesn't exist!"
End If
On Error GoTo ErrHandler
回答by Dante May Code
A version without error-handling:
没有错误处理的版本:
Function sheetExists(sheetToFind As String) As Boolean
sheetExists = False
For Each sheet In Worksheets
If sheetToFind = sheet.name Then
sheetExists = True
Exit Function
End If
Next sheet
End Function
回答by Tim Williams
There's no built-in function for this.
没有为此提供内置函数。
Function SheetExists(SheetName As String, Optional wb As Excel.Workbook)
Dim s As Excel.Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set s = wb.Sheets(SheetName)
On Error GoTo 0
SheetExists = Not s Is Nothing
End Function
回答by jogrohs
also a slightly different version. i just did a appllication.sheets.count to know how many worksheets i have additionallyl. well and put a little rename in aswell
也是一个略有不同的版本。我只是做了一个 appllication.sheets.count 来知道我还有多少工作表。好吧,并在其中进行一些重命名
Sub insertworksheet()
Dim worksh As Integer
Dim worksheetexists As Boolean
worksh = Application.Sheets.Count
worksheetexists = False
For x = 1 To worksh
If Worksheets(x).Name = "ENTERWROKSHEETNAME" Then
worksheetexists = True
'Debug.Print worksheetexists
Exit For
End If
Next x
If worksheetexists = False Then
Debug.Print "transformed exists"
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "ENTERNAMEUWANTTHENEWONE"
End If
End Sub
回答by Erik K.
Another versionof the function without error handling. This time it is not case sensitive and a little bit more efficient.
没有错误处理功能的另一个版本。这次不区分大小写,效率更高一点。
Function WorksheetExists(wsName As String) As Boolean
Dim ws As Worksheet
Dim ret As Boolean
wsName = UCase(wsName)
For Each ws In ThisWorkbook.Sheets
If UCase(ws.Name) = wsName Then
ret = True
Exit For
End If
Next
WorksheetExists = ret
End Function
回答by ShamBhagwat
Slightly changed to David Murdoch's code for generic library
稍微更改为大卫默多克的通用库代码
Function HasByName(cSheetName As String, _
Optional oWorkBook As Excel.Workbook) As Boolean
HasByName = False
Dim wb
If oWorkBook Is Nothing Then
Set oWorkBook = ThisWorkbook
End If
For Each wb In oWorkBook.Worksheets
If wb.Name = cSheetName Then
HasByName = True
Exit Function
End If
Next wb
End Function