excel - 通过宏删除工作表

标签 excel vba

我正在尝试删除除第一张表之外的所有表,但我得到一个

error 9 "subscript out of range" appears.



我该如何解决?提前致谢。
Dim Udalenie As Integer 
   If ThisWorkbook.Worksheets.Count > 1 Then
      For Udalenie = 2 To ThisWorkbook.Worksheets.Count
         ThisWorkbook.Sheets(Udalenie).Delete
   Next Udalenie
End If

最佳答案

因为每次删除一张工作表时,Excel 都会从 Worksheets 的这张工作表中删除它,并且总工作表数将减 1。

Dim Udalenie As Integer
If ThisWorkbook.Worksheets.Count > 1 Then
For Udalenie = 2 To ThisWorkbook.Worksheets.Count
ThisWorkbook.Sheets(2).Delete
Next Udalenie
End If

关于excel - 通过宏删除工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23236884/

相关文章:

excel - 在Excel中执行嵌入的.exe对象

excel - 使用 VBA 检查日期是否在两个日期之间

html - 将数据粘贴到 MS Excel 时避免换行 (<br/>)

ms-access - 从 Access 中的 SQL Server 存储过程获取结果集

excel - 将工作簿传递给 Access VBA 中的函数时出现错误 424

vba - 在宏中指定 Action 顺序?

excel - vba打开csv文件困惑的日期格式

excel - 如何从 VBA 更改列名

c# - 如何获取合并单元格的值?

vba - VBA 中的类(静态)方法