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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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