Find first non-space (special) character in cell

siers

New Member
Joined
Aug 21, 2014
Messages
13
Hi All,

I'm hoping to find a formula that can identify the position of the first non-space or special character in a cell.


For example, I have three cells with the following data:

Abcde efgh - ijklmn op qrs tuvwzyz

Abc dee (fgh ijklmn opqr stuv wzyz)

Abcd: eefgh ijkl mnop qrstuv wzyz


The desired formula would return the following for each cell:

12
9
5


Thanks,
Jeff
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Maybe a UDF (User Defined Function)

Alt+F11 ti open the VBEditor

Menu
Insert > Module

Paste code below in the right panel
Code:
Function GetSpChar(s As String)
    Dim i As Long
    
    For i = 1 To Len(s)
        If Mid(s, i, 1) Like "[! A-Za-z1-9]" Then
            GetSpChar = i
            Exit Function
        End If
    Next i
End Function

Excel

[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
Text​
[/td][td]
Result​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
Abcde efgh - ijklmn op qrs tuvwzyz​
[/td][td]
12​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
Abc dee (fgh ijklmn opqr stuv wzyz)​
[/td][td]
9​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
Abcd: eefgh ijkl mnop qrstuv wzyz​
[/td][td]
5​
[/td][/tr]
[/table]


Formula in B2 copied down
=GetSpChar(A2)

Hope this helps

M.
 
Upvote 0
Maybe a UDF (User Defined Function)

Alt+F11 ti open the VBEditor

Menu
Insert > Module

Paste code below in the right panel
Code:
Function GetSpChar(s As String)
    Dim i As Long
    
    For i = 1 To Len(s)
        If Mid(s, i, 1) Like "[! A-Za-z[B][COLOR="#FF0000"][SIZE=2]1[/SIZE][/COLOR][/B]-9]" Then
            GetSpChar = i
            Exit Function
        End If
    Next i
End Function
Typo alert... the highlighted 1 should be a 0
 
Upvote 0
Another way.

Code:
Function SPECIAL(s As String) As Variant
Dim RX As Object: Set RX = CreateObject("VBScript.RegExp")

With RX
    .Pattern = "[^\w\s]"
    Set matches = .Execute(s)
End With

SPECIAL = InStr(s, matches(0))

End Function
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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