循环删除工作表 [英] Deleting worksheets with looping
本文介绍了循环删除工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您能告诉我为什么只删除第二个工作表吗,但是如果我关闭worksheets.delete行,则会在消息框中显示所有工作表名称.
Could you tell me someone why just every second sheet is deleted, however if I turn off the worksheets.delete line than in the message box appears all the sheet names.
Sub tor()
Dim wsz As Integer
wsz = Application.Worksheets.Count
For i = 2 To wsz
MsgBox Application.Worksheets(i).Name
Application.DisplayAlerts = False
Application.Worksheets(i).Delete
Next i
End Sub
推荐答案
发生的事情是,当您从Worksheets集合中删除一个工作表时,下一个工作表将采用刚删除的工作表的索引.然后,您递增i
,跳过您实际要删除的下一张纸.
What happens is that when you delete a worksheet from the Worksheets collection, the next sheet takes the index of the one you just deleted. You then increment i
, skipping the next sheet that you actually wanted to delete.
最简单的解决方案是从头开始删除工作表.
The easiest solution is to delete worksheets beginning from the end.
For i = ThisWorkbook.Worksheets.Count To 2 Step -1
Application.Worksheets(i).Delete
Next i
这篇关于循环删除工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文