优化宏:表格内容清晰

时间:2018-07-03 14:07:01

标签: excel excel-vba vba

我正在处理很多Excel表。 当我尝试公式,其他宏等时,我使用一个非常简单的宏来清除表... 由于我不想删除指定的表,因此将它们锁定。 我需要清除5000到10000个单元格,这很长一段时间,因为我的计算机不是真正为执行此任务而构建的。

这是我的实际宏,该宏运行良好,但是是否可以更改某些内容以便更快地运行? 我知道要一一检查所有单元格很长,但是我真的有选择吗? 我必须指出,表已完全清除或根本没有清除(锁定) 而且我无法精确确定要清除或不清除的表的名称,因此,我选择浏览每个表并仅在未锁定时进行清理。

Sub RaZ_activesheet_table()
Dim tbl As ListObject
Dim retour As Long
Dim c As Range
Application.ScreenUpdating = False
retour = MsgBox(Prompt:="Vider les tableaux?", Buttons:=vbOKCancel)
If retour = vbOK Then
For Each tbl In ActiveSheet.ListObjects
    For Each c In Range(tbl.Name)
        If c.Locked = False Then
            c.ClearContents
        End If
    Next c
Next tbl
End If
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:2)

我找到了这个解决方案。 可以的话:

  1. 您首先解锁所有单元格
  2. 接下来,您可以保护不想清除的单元格
  3. 最后,您需要同时使用第一个选项添加工作表保护

enter image description here

之后,您可以应用以下简单代码:

sub test()
On Error Resume Next
    ActiveSheet.UsedRange.Value = vbNullString
On Error GoTo 0
end sub

在这种情况下,您会遇到类似这样的情况:

Sub RaZ_activesheet_table()

Dim tbl As ListObject
Dim retour As Long
Dim c As Range

Application.ScreenUpdating = False

retour = MsgBox(Prompt:="Vider les tableaux?", Buttons:=vbOKCancel)

    If retour = vbOK Then
        For Each tbl In ActiveSheet.ListObjects

            On Error Resume Next
                tbl.UsedRange.Value = vbNullString
            On Error GoTo 0

        Next tbl
    End If

Application.ScreenUpdating = True
End Sub