VBA-工作表的方法“删除"失败 [英] VBA - Method 'delete' of worksheet failing

查看:108
本文介绍了VBA-工作表的方法“删除"失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个VBA程序,现在一切都完成了,我需要彻底清理工作簿才能多次运行该程序.为此,我使用以下代码:

I made a VBA program and now that everything is done I need to clean my workbook completely to run the program several times. For this I am using this code:

Sub deleteWorksheets()
Dim ws As Worksheet

Application.DisplayAlerts = False

For Each ws In ThisWorkbook.Worksheets
        ws.Delete
Next ws

Application.DisplayAlerts = True
End Sub

但是方法删除每次都会失败.我曾尝试过激活并设置visible属性,但似乎没有任何作用.

But the method delete is failing every time. I tried to activate before and set the visible property, nothing seems to be working.

推荐答案

您需要让至少一个工作表在清除后保留下来.您不能全部删除它们.

You need to let at least one worksheet survive the purge. You can't delete them all.

尝试这样的事情:

For Each ws In ThisWorkbook.Worksheets
    If ThisWorkbook.Worksheets.Count > 1 Then
        ws.Delete
    Else
        ' in case you want to wipe out whatever was in the sheet
        ws.Cells.Clear
    End If
Next ws

这篇关于VBA-工作表的方法“删除"失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆