• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to find first character including whitespace in string

Good afternoon, I disturb your attention
I found the FirstNonDigit(xStr As String) function on the Internet, but it does not count blank spaces, as you can see in the attached file
I would appreciate modifying, or making a new, macro or function, so that it takes into account the spaces, and also shows what is the character found in the attached cell to the right
Thank you
 

Attachments

Many ways of doing this. In the first function below you define what you want to count as numeric in the line:
NotOneOfThese = "1234567890 "
and if you want to include other kinds of white space you can make it:
NotOneOfThese = "1234567890 " & vbLf & vbCr & vbTab & Chr(160)
even adding unicode characters:
NotOneOfThese = "1234567890 " & vbLf & vbCr & vbTab & Chr(160) & ChrW(8194)
[Chr(160) is a non-breeaking space and chrW(8194) is an en-space] or puctuation such as a comma, full stop decimal separator, thousands separator etc.

If that gets to be too long-winded, you could do the reverse and look for characters you want to count as non-numeric as in the second function below in the line:
AnyOfThese = "abcdefghijklmnopqrstuvwxyz"
In that function I've used SEARCH instead of FIND because it's case-insensitive.

To show what that character is in column B a formula such as:
=MID(A1,C1,1)
copied down will do that, or avoid the extra column with:
=MID(A1,FirstNonDigit(A1),1)
or
=MID(A1,FirstNonDigit2(A1),1)
Code:
Function FirstNonDigit(xStr)
Application.Volatile
NotOneOfThese = "1234567890 " & vbLf & vbCr & vbTab & Chr(160) & ChrW(8194)
For I = 1 To Len(xStr)
  If IsError(Application.Find(Mid(xStr, I, 1), NotOneOfThese, 1)) Then
    FirstNonDigit = I
    Exit For
  End If
Next I
End Function
Code:
Function FirstNonDigit2(xStr)
Application.Volatile
AnyOfThese = "abcdefghijklmnopqrstuvwxyz"
For I = 1 To Len(xStr)
  If Not IsError(Application.Search(Mid(xStr, I, 1), AnyOfThese, 1)) Then
    FirstNonDigit2 = I
    Exit For
  End If
Next I
End Function
See attached too.
 

Attachments

Back
Top