返回列表 上一主題 發帖

[發問] 刪除部分工作表

本帖最後由 luhpro 於 2014-10-13 21:32 編輯

回復 4# asch2007
那是因為 在
  sht.Delete
這一行時 sht 物件被刪掉了.

但是 For Next 迴圈還是繼續在執行,
於是跑到
If sht.Name ...
這一行時 Sht 物件已經不存在所以 "找不到物件".

底下兩種方式都可以不再出現此錯誤訊息.
  1.   Application.DisplayAlerts = False
  2.   For Each sht In Worksheets
  3.     For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
  4.       If Not IsEmpty(sht) Then
  5.         If sht.Name = Cells(i, 1) Then sht.Delete
  6.       End If
  7.     Next
  8.   Next
  9.   Application.DisplayAlerts = True
複製代碼
  1.   Application.DisplayAlerts = False
  2.   For Each sht In Worksheets
  3.     For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
  4.       If sht.Name = Cells(i, 1) Then
  5.         sht.Delete
  6.         Exit For
  7.       End If
  8.     Next
  9.   Next
  10.   Application.DisplayAlerts = True
複製代碼

TOP

        靜思自在 : 閒人無樂趣,忙人無是非。
返回列表 上一主題