Trim to eliminate spaces for a few columns via VBA

monmon

Board Regular
Joined
Apr 1, 2013
Messages
84
Hi,

I have the following code that helps me to trim certain columns on a worksheet via VBA. The code is part of the macro that I am running. However, the VBA trim code causes the entire macro to run at a very slow pace.

Is it possible to modify the codes such that it will run faster?

Thanks in advance!

Code:
Dim RngZ As Range, C2 As Range, D2 As Range, RngM As Range, RngB As Range, D3 As Range

Set RngZ = Range("B5", Range("D" & Rows.Count).End(xlUp))

For Each C2 In RngZ
C2.Value = Application.Trim(C2.Value)

Next C2

Set RngM = Range("J5", Range("L" & Rows.Count).End(xlUp))

For Each D2 In RngM
D2.Value = Application.Trim(D2.Value)

Next D2
 
There is nothing wrong with your codes and I appreciate that you've taken valuable time in explaining and teaching me.

I realised that my codes and all of your contributed codings do take care of blank cells within a certain range.

However, the .End(XlUp).Row only allows a certain range to be scanned for trim.

For example, the range B5 to D10. If I've a data in C100, then C100 will not be scanned for trim.

I'm wondering if I want to trim the whole of Columns B, C and D, how should I change the codes and at the same time allow it to run faster.
Here is my code modified to find the last row in each range no matter what column that last row is in...

Code:
Sub TrimColumnsB2DandJ2L()
  Dim Addr As String
  Addr = "B5:D" & Range("B:D").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
  Range(Addr) = Evaluate("IF(ROW(),TRIM(" & Addr & "),"""")")
  Addr = "J5:L" & Range("J:L").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
  Range(Addr) = Evaluate("IF(ROW(),TRIM(" & Addr & "),"""")")
End Sub
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
monmon,

I'm wondering if I want to trim the whole of Columns B, C and D

Based on your latest request, the following should do what you have requested.

Code:
Option Explicit
Sub TrimRangeV2()
' hiker95, 02/05/2014, ME755373
Dim lr As Long
lr = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
With Range("B1:D" & lr)
  .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
End Sub
 
Last edited:
Upvote 0
Here is my code modified to find the last row in each range no matter what column that last row is in...

Code:
Sub TrimColumnsB2DandJ2L()
  Dim Addr As String
  Addr = "B5:D" & Range("B:D").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
  Range(Addr) = Evaluate("IF(ROW(),TRIM(" & Addr & "),"""")")
  Addr = "J5:L" & Range("J:L").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
  Range(Addr) = Evaluate("IF(ROW(),TRIM(" & Addr & "),"""")")
End Sub

Hi Rick,

This works very well! thanks for helping me!!
 
Upvote 0
monmon,



Based on your latest request, the following should do what you have requested.

Code:
Option Explicit
Sub TrimRangeV2()
' hiker95, 02/05/2014, ME755373
Dim lr As Long
lr = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
With Range("B1:D" & lr)
  .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
End Sub

Hi Hiker,

Your code works well too. Thanks everyone for the great assistance!!
 
Upvote 0
monmon,

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.
 
Upvote 0
Hi,

Can anyone explain why the IF is necessary in the Evaluate formula? How does that behave differently than:
Code:
Range(Addr) = Evaluate("TRIM(" & Addr & ")")

Thanks,
 
Upvote 0
The If(Istext is testing if the value in the range is TEXT.

You don't want to apply TRIM to a Numeric Value.
It will convert it to a "Number Stored As Text"
 
Upvote 0
Jonmo1,

Thanks for the quick response! That makes sense. What was confusing me is the ...IF(ROW()... version, I still don't follow that one.
 
Upvote 0
Disregard that...
The .Value converts the "number stored as text" to a real number..

But the Evaluate function needs something to make it behave like an array.
Otherwise, all cells end up containing the same value (the value in the top/left cell of the range)


The Row or IsText functions make Evaluate treat the formula as an array.
Trim by itself will not.
 
Upvote 0

Forum statistics

Threads
1,216,180
Messages
6,129,341
Members
449,505
Latest member
Alan the procrastinator

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