Excel VBA如果存在WorkSheet(“ wsName”)

我想知道是否存在干净的功能,如果工作簿中的工作表存在,则返回True或False?


如果可以在不跳过错误处理的情况下做到这一点,那将是很好,但不是必须的。


我发现的唯一东西并没有真正起作用:


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


智慧大石
浏览 1170回答 3
3回答

温温酱

没有内置功能。Function SheetExists(SheetName As String, Optional wb As Excel.Workbook)&nbsp; &nbsp;Dim s As Excel.Worksheet&nbsp; &nbsp;If wb Is Nothing Then Set wb = ThisWorkbook&nbsp; &nbsp;On Error Resume Next&nbsp; &nbsp;Set s = wb.Sheets(SheetName)&nbsp; &nbsp;On Error GoTo 0&nbsp; &nbsp;SheetExists = Not s Is NothingEnd Function
打开App,查看更多内容
随时随地看视频慕课网APP