Tập lệnh sau hoạt động trên các tập dữ liệu nhỏ hơn (ít hơn 30k hàng hoặc hơn), nhưng dẫn đến lỗi "#VALUE" cho mỗi ô trong phạm vi đã chọn khi phạm vi lớn hơn phạm vi đó.VBA và Excel: Tại sao tập lệnh TRIM của tôi dẫn đến #VALUE trên các tập dữ liệu lớn?
Dim FirstCell As Range, LastCell As Range, MyRange As Range
Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
Set FirstCell = Cells(Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues).Row, _
Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, LookIn:=xlValues).Column)
Set MyRange = Range(FirstCell, LastCell)
MyRange.Select
If MyRange Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Selection
.Value = Evaluate("if(row(" & .Address & "),clean(trim(" & .Address & ")))")
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Finished trimming " & vbCrLf & "excess spaces", 64
Khoảng bao nhiêu ô bạn đang thay đổi với mã này? Tôi đã thử nghiệm nó trên ~ 1,7 triệu tế bào và tôi đã không nhận được bất kỳ lỗi nào – TMH8885
8,106,860 ô trên tài liệu thực hành của tôi. Đó là 161,363 hàng. –
Dường như nó hoạt động tối đa khoảng 70 nghìn hàng. Quá nhiều hơn thế, nó chỉ đặt giá trị của mỗi ô trong phạm vi được chọn thành #VALUE. –