在进行之前检查其他工作簿的工作表名称

我有这个很好的代码。

除了列表中的其他纸张没有分配的纸张名称时,它什么也不做。 所以我想添加一个代码,该代码会有一个popup窗口,提示“工作簿中不存在工作表名称”。

我已尽可能多地尝试了代码,但似乎没有任何工作。 这最后一个工程, 它有一个popup式工作簿中,而不是我正在寻找的工作表中的每个工作表。

我怎样才能编辑这个,所以点击后,代码将search另一个工作簿,确定工作表中是否有多less工作表,只有一个popup窗口说不是?

谢谢

Private Sub CopyPasteButton_Click() ActiveSheet.Unprotect Password:=PSWD Dim mySheet As Worksheet, otherSheet As Worksheet Dim ws As Worksheet On Error GoTo exit_err Application.DisplayAlerts = False Set mySheet = ThisWorkbook.Sheets("Info") For Each ws In Workbooks(Me.ListBox1.Value).Worksheets If ws.Name = "This is It" Then Set otherSheet = Workbooks(Me.ListBox1.Value).Sheets("This is It") If otherSheet.Range("AN1") >= 148 Then mySheet.Range("A50:J57").Copy otherSheet.Range("A5:J12").PasteSpecial xlPasteValuesAndNumberFormats mySheet.Range("M6:N6").Copy otherSheet.Range("Q19:R19").PasteSpecial xlPasteValuesAndNumberFormats Else MsgBox "Wrong Sheet Version" End If Else MsgBox "Sheet Does not Exist" End If Next ws exit_err: mySheet.Protect Password:=PSWD Application.DisplayAlerts = True Application.ScreenUpdating = True Application.CutCopyMode = False End Sub Function WorksheetExists(ByValWorksheetName As String) As Boolean Dim Sht As Worksheet For Each Sht In Workbooks(Me.ListBox1.Value).Worksheets If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then WorksheetExists = True Exit Function End If Next Sht WorksheetExists = False End Function 

不要遍历表单,只要设置表单的引用并在错误不存在的情况下捕获错误。

 Private Sub CopyPasteButton_Click() ActiveSheet.Unprotect Password:=pswd Dim mySheet As Worksheet, otherSheet As Worksheet Dim ws As Worksheet On Error GoTo exit_err Application.DisplayAlerts = False Set mySheet = ThisWorkbook.Sheets("Info") On Error Resume Next Set otherSheet = Workbooks(Me.ListBox1.Value).Worksheets("This is It") On Error GoTo 0 If Not otherSheet Is Nothing Then If otherSheet.Range("AN1") >= 148 Then mySheet.Range("A50:J57").Copy otherSheet.Range("A5:J12").PasteSpecial xlPasteValuesAndNumberFormats mySheet.Range("M6:N6").Copy otherSheet.Range("Q19:R19").PasteSpecial xlPasteValuesAndNumberFormats Else MsgBox "Wrong Sheet Version" End If Else MsgBox "Sheet Does not Exist" End If exit_err: mySheet.Protect Password:=pswd Application.DisplayAlerts = True Application.ScreenUpdating = True Application.CutCopyMode = False End Sub