Counting the number of blanks removed by trim

rob737

Board Regular
Joined
May 12, 2015
Messages
129
Hi Forum

Hope all is well, would like a bit of help with a macro that trims spaces from cells.

I found this snippet and works great, however I would like to count the number of blanks the macro removes.

Sub NoSpaces()
Dim c As Range
For Each c In Selection.Cells
c = Trim(c)
Next
End Sub



As always would appreciate any help you can provide

Many thanks
Regards
Rob
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You could check the length of the string in "c" before and after trimming.
 
Last edited:
Upvote 0
Hi Forum

Hope all is well, would like a bit of help with a macro that trims spaces from cells.

I found this snippet and works great, however I would like to count the number of blanks the macro removes.

Code:
Sub NoSpaces()
  Dim c As Range[B][COLOR="#FF0000"], TotalLen As Long, TrimCount As Long[/COLOR][/B]
  For Each c In Selection.Cells
    [B][COLOR="#FF0000"]TotalLen = Len(c)[/COLOR][/B]
    c = Trim(c)
    [B][COLOR="#FF0000"]TrimCount = TotalLen - Len(c)[/COLOR][/B]
    [B][COLOR="#FF0000"]MsgBox "Number of characters trimmed: " & TrimCount[/COLOR][/B]
  Next
End Sub
Try adding the lines of code I show above.
 
Upvote 0
Try adding the lines of code I show above.

Hi Rick

Great many thanks, the code counts the number of blanks removed for each cell which is great, how can I adjust it to get one messagebox for a total of all the cells as a total from the sheet.

As always any help would be greatly appreciated

Thanks
 
Upvote 0
Maybe something like this.
And for a quickie test use cells B1:B5 as a "selected" test range with C1 & C2 = lens before and after and column D & E show before and after for each row.

Howard

Code:
Option Explicit

Sub NoSpaces()
 Dim c As Range
 
 For Each c In Selection.Cells
 
   [C1] = [C1] + Len(c)
   c.Offset(, 2) = Len(c)
   
   c = Trim(c)
 
   c.Offset(, 3) = Len(c)
   [C2] = [C2] + Len(c)
   
 Next
 
 MsgBox [C1] - [C2] & " Spaces removed"
 
 End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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