我在每张纸上都有多张数据表。我想删除第4页到无限制的表格。我试过下面的代码:
Sub Delete_Sheets()
Application.ScreenUpdating = False
Dim j As Integer
j = Worksheets.Count
For k = 4 To j
With Sheets(k).Delete
End With
Next k
Application.ScreenUpdating = True
End Sub
问题是,每次代码都要求我允许删除工作表,最后只看到删除几张工作表。有人帮忙。
答案 0 :(得分:4)
关闭显示的提醒。从最后一张纸向后删除。
Sub Delete_Sheets()
Dim j as long
Dim k As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
j = Worksheets.Count
For k = j To 4 Step -1
Sheets(k).Delete
Next k
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
答案 1 :(得分:3)
关闭确认并循环删除工作表命令,直到剩下三个工作表。
Option Explicit
Sub qwea()
Application.DisplayAlerts = False
Do While Worksheets.Count > 3
Worksheets(Worksheets.Count).Delete
Loop
Application.DisplayAlerts = True
End Sub
如果您将For ... Next的增量反转为For k = j To 4 step - 1
,那么您的代码将正常工作。你在删除时“踩过”了工作表。
答案 2 :(得分:0)
如果你删除的索引4以后的纸张,那么也许这对你有用。
Dim idx, s As String
Application.DisplayAlerts = False '/* of course you need to turn this off */
If Sheets.Count > 3 Then
s = "=TRANSPOSE(ROW(A4:A" & Sheets.Count & "))"
idx = Evaluate(s)
Sheets(idx).Delete
End If
Application.DisplayAlerts = True
这将始终删除超过4张 但请确保您的工作表始终正确编入索引,否则请使用循环。