Find Char (one of) in String

RayBan

New Member
Joined
Dec 22, 2016
Messages
37
Is there an elegant way to see if a string contains ONE "A" (instead of more than one)?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Via formula:
=LEN(A1)-LEN(SUBSITUTE(A1,"A",""))

So..

=IF(LEN(A1)-LEN(SUBSTITUTE(A1,"A","")) = 1,Do Something If True,Do Something If False)

if you want it to be case sensitive...good question.

if you want to do it via VBA:

If Len(Range("A1").Value) - Len(Replace(Range("A1").Value, "a", "")) = 1 Then
Do Something If True
Else
Do Something If False
End If
 
Upvote 0
This works, although it might not be elegant enough for you:

Excel 2016 (Windows) 32 bit
AB
1BananaTRUE

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
B1=IF(LEN(A1)-LEN(SUBSTITUTE(A1,"a",""))=1,TRUE,FALSE)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
Upvote 0
This works, although it might not be elegant enough for you:

Excel 2016 (Windows) 32 bit
AB
1BananaTRUE

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
B1=IF(LEN(A1)-LEN(SUBSTITUTE(A1,"a",""))=1,TRUE,FALSE)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
If the goal is to return TRUE or FALSE, then this is enough...

=LEN(A1)-LEN(SUBSTITUTE(A1,"a",""))>1
 
Upvote 0
If the goal is to return TRUE or FALSE, then this is enough...

=LEN(A1)-LEN(SUBSTITUTE(A1,"a",""))>1
I can't believe I typed a greater than symbol instead of an equal sign! :oops:

This is what I meant to write...

=LEN(A1)-LEN(SUBSTITUTE(A1,"a",""))=1
 
Last edited:
Upvote 0
Neither can I, Rick, but it's exactly what I did first time - notice that I edited my post just before you quoted it!!! ;)
 
Upvote 0
Thanks very much, one and all. It was extremely helpful. I wasn't using a worksheet and never thought to say so. (Sorry)
But from your advice I have

Code:
Function ContainsJustOne(testString As String, Char As String) As Boolean
    If Len(testString) - Len(Replace(testString, Char, "")) = Len(Char) Then ContainsJustOne = True
End Function

It seems to be working just great and with the added bonus of being case sensitive.
Changing to '=Len(Char)' from '=1' so far tests ok where Char > 1. (Wasn't wanted but makes it better)
Hopefully I have it right!

Thanks all.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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