2016-02-02 20 views
5

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 

VBA TRIM Error

+0

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

+0

8,106,860 ô trên tài liệu thực hành của tôi. Đó là 161,363 hàng. –

+0

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. –

Trả lời

1

tôi quản lý để tái tạo vấn đề của bạn, và sử dụng một mảng biến thể như hình dưới đây vượt qua vấn đề này đối với dữ liệu lớn đặt

Dim FirstCell As Range, LastCell As Range, MyRange As Range 
Dim DataRange() As Variant 
Dim lRows As Long 
Dim lCols As Long 
Dim i As Long, j As Long 
Dim value As String 

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 
    lRows = MyRange.Rows.Count 
    lCols = MyRange.Columns.Count 
    ReDim DataRange(1 To lRows, 1 To lCols) 
    DataRange = MyRange.value 
    For j = 1 To lCols 
    For i = 1 To lRows 
     DataRange(i, j) = Trim(DataRange(i, j)) 
    Next i 
    Next j 

    MyRange.value = DataRange 

Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 
MsgBox "Finished trimming " & vbCrLf & "excess spaces", 64 

Để tham khảo, tôi đã sử dụng bài viết này để giúp đưa ra câu trả lời: https://blogs.office.com/2008/10/03/what-is-the-fastest-way-to-scan-a-large-range-in-excel/

+0

Liên kết có thể bị hỏng trong tương lai. Vui lòng sao chép-dán thông tin cần thiết để trả lời câu hỏi vào bài đăng của bạn. Chỉ sử dụng liên kết làm tham chiếu. – eirikdaude

+0

Tôi đã cập nhật câu trả lời để làm cho nó rõ ràng hơn rằng liên kết chỉ để tham khảo, câu trả lời được bao gồm ở trên – TrtlBoy

Các vấn đề liên quan