Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90301

Excel VBA fast trim of range with evaluate not working

$
0
0

I'm trying to speed up the trimming of a data sheet with about 5000 rows and 12 columns.

I've used a For Each Loop that works but always sets Excel into "not responding" for a while before it's done. Now I've done some research and found that With and Evaluate apparently works faster but I can't seem to get it working for me. It ends up deleting a bunch of my data and resorting the rest of it.

Here is my old code that works but is slow:

'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

This is my new code that is fast but destroys my data:

'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

Any idea what is wrong with my second code or other ideas on how I can speed up the first code?

Thanks!


Viewing all articles
Browse latest Browse all 90301

Trending Articles