Countif with trim

West Man

Well-known Member
Joined
Mar 27, 2006
Messages
1,175
I wish to do a countif trimming both the range and the criteria and don't know how to trim the range. =countif(A$2:A$10,TRIM(A2) but with a Trim of A2:A10. In other words a countif ignoring leading and trailing spaces.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You will have better luck and fewer headaches if you just remove the trailing/leading spaces to begin with.

Here's a macro you can use to do that for you

Code:
Sub test()
Application.ScreenUpdating = False
For Each Cell In ActiveSheet.UsedRange
If Cell.HasFormula = False Then
    Cell.Value = Strings.Trim(Cell.Value)
End If
Next Cell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I wish to do a countif trimming both the range and the criteria and don't know how to trim the range. =countif(A$2:A$10,TRIM(A2) but with a Trim of A2:A10. In other words a countif ignoring leading and trailing spaces.

=SUMPRODUCT((TRIM(A$2:A$10)=TRIM(E2))+0)

where E2 houses the thing to count.

Cheaper: Try to run ASAP Utilities (asap-utilities.com) on A2:A10 to remove excessive spaces.
 
Upvote 0
Thank you Jonmo. I have an add-in from MacroSysyems that has an equivalent macro. This is for another user who does not have the add-in, so your code is helpful.

Aladin, spot on as always.

Thank you both.
 
Upvote 0

Forum statistics

Threads
1,223,226
Messages
6,170,844
Members
452,360
Latest member
abishekjmichael

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