测试或检查是否存在工作表

测试或检查是否存在工作表

Dim wkbkdestination As WorkbookDim destsheet As WorksheetFor Each ThisWorkSheet In wkbkorigin.Worksheets 
    'this throws subscript out of range if there is not a sheet in the destination 
    'workbook that has the same name as the current sheet in the origin workbook.
    Set destsheet = wkbkdestination.Worksheets(ThisWorkSheet.Name) Next

基本上,我循环遍历原始工作簿中的所有工作表,然后设置destsheet在目标工作簿中,指向与原版工作簿中当前迭代的工作表名称相同的工作表。

我怎样才能测试那张纸是否存在?类似于:

If wkbkdestination.Worksheets(ThisWorkSheet.Name) Then


繁华开满天机
浏览 805回答 3
3回答

Cats萌萌

有些人不喜欢这种方法,因为错误处理的使用“不适当”,但我认为它在VBA中是可以接受的.另一种方法是循环遍历所有的工作表,直到找到匹配的页为止。Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean     Dim sht As Worksheet    If wb Is Nothing Then Set wb = ThisWorkbook    On Error Resume Next     Set sht = wb.Sheets(shtName)     On Error GoTo 0     WorksheetExists = Not sht Is NothingEnd Function
打开App,查看更多内容
随时随地看视频慕课网APP