Extract non-alphanumeric character and all alphanumeric to right of non-alphanumeric

alb58

New Member
Joined
May 16, 2014
Messages
2
Hi,

I have been fighting with this for a few days. I am trying to find the first non-alphanumeric character in a cell then remove that character and all characters following. The data I am working with is in a csv file and looks as follows:

Bus Stop
Assembly / Ex...
In Class
Yard, Corrido...

I want to remove everything after and including "/" as well as everything after and including "," so the data needs to end up looking like:

Bust Stop
Assembly
In Class
Yard

Once I have cleaned up the data it is going to be copied to another worksheet and, as the original csv file will not be kept, there can't be a link back to the original worksheet.

I have used =LEFT(D24,FIND("/",D24)-1) which works on the cells with "/" in them but returns #VALUE (of course) for the cells with "," in them. In an attempt to allow for the non-alphanumeric character being either / or , I have tried using IF with LEFT, I have tried OR with IF and LEFT, I have tried using an array {"/",","} but have not been able to make anything work. I would really appreciate assistance with this problem.

Thank you.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
B2:

=TRIM(LEFT(A2,LOOKUP(9.99999999999999E+307,SEARCH({"/",","},A2&"/")-1)))

where A2 houses a string to process.
 
Upvote 0
In addition. I see a lot of questions about replacing characters. I think the user defined and very versatile function "dhTranslate" from Ken Getz from the book "VBA Developer's Handbook" comes in very handy and never let me down. I think it's pretty much self explaining. Adjust to suit your needs.

Dutch version (delimiter is semicolon): Formula [B2] =dhTranslate(A2;"./,…";"")
English version (delimiter is comma): Formula [B2] =dhTranslate(A2,"./,…","")


Code:
Public Function dhTranslate(ByVal strIn As String, _
ByVal strMapIn As String, _
ByVal strMapOut As String, _
Optional lngCompare As VbCompareMethod = vbBinaryCompare) As String
' In:
'   strIn:
'       String in which to perform replacements
'   strMapIn:
'       Map of characters to find
'   strMapOut:
'       Map of characters to replace.  If the length
'       of this string is shorter than that of strMapIn,
'       use the final character in the string for all
'       subsequent matches.
' Example:
'   dhTranslate("This is a test", "aeiou", "AEIOU") returns
'     "ThIs Is A tEst"


    Dim lngI As Long
    Dim lngPos As Long
    Dim strChar As String * 1
    Dim strOut As String


    ' If there's no list of characters
    ' to replace, there's no point going on
    ' with the work in this function.
    If Len(strMapIn) > 0 Then
        ' Right-fill the strMapOut set.
        If Len(strMapOut) > 0 Then
            strMapOut = Left$(strMapOut & String(Len(strMapIn), _
            Right$(strMapOut, 1)), Len(strMapIn))
        End If


        For lngI = 1 To Len(strIn)
            strChar = Mid$(strIn, lngI, 1)
            lngPos = InStr(1, strMapIn, strChar, lngCompare)
            If lngPos > 0 Then
                ' If strMapOut is empty, this doesn't fail,
                ' because Mid handles empty strings gracefully.
                strOut = strOut & Mid$(strMapOut, lngPos, 1)
            Else
                strOut = strOut & strChar
            End If
        Next lngI
    End If
    dhTranslate = strOut
End Function
© Ken Getz & Mike Gilbert 1998

 
Upvote 0
In addition. I see a lot of questions about replacing characters. I think the user defined and very versatile function "dhTranslate" from Ken Getz from the book "VBA Developer's Handbook" comes in very handy and never let me down. I think it's pretty much self explaining. Adjust to suit your needs.
I think this somewhat shorter code will do the same thing as the code you posted does (for Excel only)...
Code:
Function rrTranslate(ByVal strIn As String, _
                     ByVal strMapIn As String, _
                     ByVal strMapOut As String, _
                     Optional lngCompare As VbCompareMethod = vbBinaryCompare) As String
  Dim X As Long
  For X = 1 To Len(strMapIn)
    strIn = Replace(strIn, Mid(strMapIn, X, 1), Mid(strMapOut, Application.Max(1, Application.Min(X, Len(strMapOut))), 1))
  Next
  rrTranslate = strIn
End Function
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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