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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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

A
B
1
Text​
Result​
2
Abcde efgh - ijklmn op qrs tuvwzyz​
12​
3
Abc dee (fgh ijklmn opqr stuv wzyz)​
9​
4
Abcd: eefgh ijkl mnop qrstuv wzyz​
5​

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,214,415
Messages
6,119,381
Members
448,888
Latest member
Arle8907

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