Test or check if sheet exists

后端 未结 22 2846
深忆病人
深忆病人 2020-11-21 23:51
Dim wkbkdestination As Workbook
Dim destsheet As Worksheet

For Each ThisWorkSheet In wkbkorigin.Worksheets 
    \'this throws subscript out of range if there is not         


        
22条回答
  •  萌比男神i
    2020-11-22 00:21

    I came up with an easy way to do it, but I didn't create a new sub for it. Instead, I just "ran a check" within the sub I was working on. Assuming the sheet name we're looking for is "Sheet_Exist" and we just want to activate it if found:

    Dim SheetCounter As Integer
    
    SheetCounter = 1
    
    Do Until Sheets(SheetCounter).Name = "Sheet_Exist" Or SheetCounter = Sheets.Count + 1
     SheetCounter = SheetCounter +1
    Loop
    If SheetCounter < Sheets.Count + 1 Then
     Sheets("Sheet_Exist").Activate
    Else
     MsgBox("Worksheet ""Sheet_Exist"" was NOT found")
    End If
    

    I also added a pop-up for when the sheet doesn't exist.

提交回复
热议问题