使用VBA在Excel中删除工作表 [英] Delete worksheet in Excel using VBA
本文介绍了使用VBA在Excel中删除工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个宏,它可以生成许多工作簿.我希望宏在运行开始时检查文件是否包含2个电子表格,并删除它们(如果存在).
I have a macros that generates a number of workbooks. I would like the macros, at the start of the run, to check if the file contains 2 spreadsheets, and delete them if they exist.
我尝试的代码是:
If Sheet.Name = "ID Sheet" Then
Application.DisplayAlerts = False
Sheet.Delete
Application.DisplayAlerts = True
End If
If Sheet.Name = "Summary" Then
Application.DisplayAlerts = False
Sheet.Delete
Application.DisplayAlerts = True
End If
此代码返回错误:
运行时错误#424,需要对象.
run time error #424, object required.
我可能使用了错误的格式,但是如果有更简单的方法可以这样做,那将非常有用.
I probably have the wrong formatting, but if there is an easier way to do this, it would be very useful.
推荐答案
考虑:
Sub SheetKiller()
Dim s As Worksheet, t As String
Dim i As Long, K As Long
K = Sheets.Count
For i = K To 1 Step -1
t = Sheets(i).Name
If t = "ID Sheet" Or t = "Summary" Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
End If
Next i
End Sub
注意:
因为我们要删除,所以我们向后运行循环.
Because we are deleting, we run the loop backwards.
这篇关于使用VBA在Excel中删除工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文