【发布时间】:2018-09-18 09:01:17
【问题描述】:
我编写此代码是为了从表中删除特定列中不包含单词“ITA”、“GRE”或“CHE”的所有行。现在,表格很大(60k 观察),循环显然很耗时(5-6 分钟)。为了优化代码效率(即在 10 到 30 秒内执行任务),处理任务的另一种方法是什么?
Sub test()
countrycol = UsedRange.Find("Country", lookat:=xlWhole).Column
For j = 1 To Cells(Rows.Count, countrycol).End(xlUp).Row
If UsedRange.Cells(j + 1, countrycol).Value <> "ITA" Or UsedRange.Cells(j + 1, countrycol).Value <> "GRE" _
Or UsedRange.Cells(j + 1, countrycol).Value <> "CHE" Then
UsedRange.Cells(j + 1, countrycol).EntireRow.Delete
End If
Next j
End Sub
【问题讨论】:
-
工作表的名称是什么?
-
设置 sht=Sheets("All")
-
供将来参考:这是找出损坏代码不起作用的地方。需要改进的功能代码(例如更短的运行时间)更适合Code Review。