Trim, Clean using Evaluate running into 256 character limit

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello I have large datasets that I run automated processes on that I need to trim and clean and have been using:

VBA Code:
Function CleanSheets(arrShtNames As Variant, startRow As Long)
Dim ws As Worksheet
Dim rng As Range
Dim LR As Long, Lc As Long
  
  For Each ws In Worksheets(arrShtNames)
   With ws
   
      Lc = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
      LR = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row  
      Set rng = .Range("A" & startRow).Resize(LR, Lc)

      rng.Replace what:=vbNullChar, Replacement:=vbNullString
      rng.Replace what:="#NULL!", Replacement:=vbNullString
      rng.Replace what:="#VALUE!", Replacement:=""
      
      rng = Evaluate("IF(" & rng.Address & "="""","""",CLEAN(TRIM(" & rng.Address & ")))")
       
   End With
  Next ws
  
End Function

for this. It executes in a few seconds if I use an array and loop through the data it takes minutes

But my recent datasets some cells exceed the character limit and these cells are given the value "#VALUE!" by my function
I want to test for the character limit and if it is exceeded then skip those cells or post back there value

I have tried (and similar things)
Code:
rng = Evaluate("IF(Len(" & rng.Address & ")<256,CLEAN(TRIM(" & rng.Address & ")), rng.adress.value)")

but not getting how to postback the value of the cell.

Thanks for any help on this
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If you just want the cell values back, it would be:

Code:
rng = Evaluate("IF(Len(" & rng.Address & ")<256,CLEAN(TRIM(" & rng.Address & "))," &  rng.address & ")")
 
Upvote 0
Then I guess you'd have to return some other text. If you want blanks it would be:

Code:
rng = Evaluate("IF(Len(" & rng.Address & ")<256,CLEAN(TRIM(" & rng.Address & ")),"""")")
 
Upvote 0
Thanks Rory, I guess I have to stop using the Evaluate.

But I just got this and it keeps the speed

VBA Code:
Function UltraCleanSheetsList(arrShtNames As Variant, startRow As Long, removeNULL As Boolean)
Dim ws As Worksheet
Dim rng As Range
Dim LR As Long, Lc As Long

  For Each ws In Worksheets(arrShtNames)
   With ws
 
    Lc = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    LR = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    Set rng = .Range("A" & startRow).Resize(LR, Lc)
   
      rng.Replace what:=vbNullChar, Replacement:=vbNullString
      rng.Replace what:="#NULL!", Replacement:=vbNullString
      rng.Replace what:="#VALUE!", Replacement:=""
      rng.value = Application.Trim(rng)
      rng.value = Application.Clean(rng)
     
   End With
  Next ws

End Function
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top