[我正在尝试加速整理约5000行和12列的数据表。
我使用了一个For Each循环,但在完成之前将Excel设置为“无响应”一段时间。
我研究发现With和Evaluate显然工作更快。对我来说,它最终会删除我的一堆数据并重新使用其余数据。
这是我的旧代码,可以运行,但是很慢:
'trim everything in range
Dim Rng As Range
Set Rng = timeSheet.Range("A2:L" & lastCell)
For Each cell In Rng
If Not IsEmpty(cell.Value) Then
cell.Value = Trim(cell)
End If
Next cell
这是我的新代码,速度很快,但会破坏我的数据:
'trim everything in range 2.0
Dim Rng As Range
Set Rng = timeSheet.Range("A2:L" & lastCell)
With Rng
.Value = Evaluate("IF(ROW(" & .Address & "),CLEAN(TRIM(" & .Address & ")))")
End With
第二个代码有什么问题,或者关于如何加快第一个代码的想法?
您可以将值放置在数组中,所以将Trim()放在那里,然后放回值。
Sub Tester()
Dim Rng As Range, r As Long, c As Long, arr, v
Set Rng = timeSheet.Range("A2:L" & lastCell)
arr = Rng.Value
For r = 1 To UBound(arr, 1)
For c = 1 To UBound(arr, 2)
v = arr(r, c)
If Len(v) > 0 Then arr(r, c) = Trim(v)
Next c
Next r
Rng.Value = arr
End Sub