加速Excel条件循环清除内容的优化方案咨询
优化批量清除联系人列内容的VBA代码,提升运行速度
我用以下简单宏清除选中联系人对应列的内容。联系人按标题行划分分类,因此需要设置条件,避免清除用于排序等操作的标题行单元格。目前有7000+条联系人数据,原宏运行需15+分钟;添加判断单元格是否已清空的条件后,运行时间缩短5-7分钟,但仍寻求更优代码以进一步加速运行。
Sub ClearSelected() a = MsgBox("WARNING: This will Clear all selected or invited contacts. Wait for notification that the operation is complete. Continue?", 4) If a = 7 Then Exit Sub End If LastRow = ActiveSheet.UsedRange.Rows.Count For i = 4 To LastRow If Cells(i, Range("CompanyName").Column).Value <> "ZZZZZZZZZ" Or Cells(i, Range("CompanyName").Column).Value <> "ZZZZZZZZY" Then Cells(i, Range("Invited").Column).ClearContents End If Next i MsgBox ("Complete") End Sub
更新说明
由于判断语句比清除内容操作更快,我添加了额外条件,若单元格已清空则跳过。运行时间根据需清除的单元格数量有所缩短,通常可减少5-7分钟。
For i = 4 To LastRow If Cells(i, Range("CompanyName").Column).Value <> "ZZZZZZZZZ" Or Cells(i, Range("CompanyName").Column).Value <> "ZZZZZZZZY" Then If Cells(i, Range("Invited").Column).Value <> "" Then Cells(i, Range("Invited").Column).ClearContents End If End If Next i
内容的提问来源于stack exchange,提问作者Neuner
相关产品推荐
相关产品推荐

