Home » excel » excel – Replace several elements in one column

excel – Replace several elements in one column

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have one column with the names (First Name, Last Name) but sometimes there are the certificates, grades etc added to the names (ex: “John Smiths MBA”; “Susan Smiths FCA, ACCA”). The number of variable is countless but I identified the most common (there are many). Please help how to build the macro to clean this?
I’ve been using one by one, with:

Selection.Replace What:=" FCA,", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=True, _
        ReplaceFormat:=False

But I guess there must be more efficient way to build this macro (and edit in case when new “unwanted extension is spotted).

How to&Answers:

Try this code

Sub Test()
Dim e, a, i As Long

Application.ScreenUpdating = False
    For Each e In Array("MBA", "FCA", "ACCA", ", ")
        Columns(1).Replace e, "", 2
    Next e

    With Range("A1", Cells(Rows.Count, "A").End(xlUp))
        .Value = Evaluate(Replace("IF(COLUMN(@)=1,TRIM(@),TRIM(PROPER(@)))", "@", .Address))
    End With
Application.ScreenUpdating = True
End Sub

Answer:

I assume all the certificates and grades are 3 to 4 letters at the end of names, and it’s all in UPPER case. Try the code based on this scenario.

Sub Test()

Dim name As String

i = 2
name = Range("A" & i).Value

While name <> ""

If Right(name, 4) = UCase(Right(name, 4)) Then
Range("A" & i).Value = Left(name, Len(name) - 4)

ElseIf Right(name, 3) = UCase(Right(name, 3)) Then
Range("A" & i).Value = Left(name, Len(name) - 3)
End If

i = i + 1
name = Range("A" & i).Value
Wend

End Sub