C.Fsh
C.Fsh

Reputation: 55

UDF that uses 3 cells

I'm trying to translate the following formula to a UDF, mainly to make it easier for people to use.

=IF(LEFT(TRIM(RIGHT(A2,LEN(A2)-LEN(B2)-LEN(C2)-2)),4)="and ",RIGHT(TRIM(RIGHT(A2,LEN(A2)-LEN(B2)-LEN(C2)-2)),LEN(TRIM(RIGHT(A2,LEN(A2)-LEN(B2)-LEN(C2)-2)))-4),TRIM(RIGHT(A2,LEN(A2)-LEN(B2)-LEN(C2)-2)))

The formula is just a conditional text remover, but it's necessary for having a visible procedure. A2 is a list of names, split by commas or "and ", B and C2 are the previous Forename and Surname which we are removing from the left of the string.

I've done a few of these, but this is the first one that uses more than one cell, and the same methods I've used before are not working. I've changed bits to use Application.WorksheetFunction and changed the cell references to A, B and C.Value2 but I can't seem to get it working.

Public Function NextName(A As Range, B As Range, C As Range)

    NextName = _
        Application.WorksheetFunction.IF(Left(Trim(Right(A.Value2, _
        Application.WorksheetFunction.Len(A.Value2) - Application.WorksheetFunction.Len(B.Value2) - _
        Application.WorksheetFunction.Len(C.Value2) - 2)), 4) = "and ", Right(Trim(Right(A.Value2, Application.WorksheetFunction.Len(A.Value2) - _
        Application.WorksheetFunction.Len(B.Value2) - Application.WorksheetFunction.Len(C.Value2) - 2)), _
        Application.WorksheetFunction.Len(Trim(Right(A.Value2, Application.WorksheetFunction.Len(A.Value2) - _
        Application.WorksheetFunction.Len(B.Value2) - Application.WorksheetFunction.Len(C.Value2) - 2))) - 4), Trim(Right(A.Value2, _
        Application.WorksheetFunction.Len(A.Value2) - Application.WorksheetFunction.Len(B.Value2) - Application.WorksheetFunction.Len(C.Value2) - 2)))

End Function

[EXAMPLES]

(A2)

John Smith Rogerson and Jane Laura Manson, Name2 Name2 Name2, Name3 Name3 Name3, Name4 Name4 Name4

(B2)

John Smith Rogerson

(C2)

Rogerson

(Result)

Jane Laura Manson, Name2 Name2 Name2, Name3 Name3 Name3, Name4 Name4 Name4

Upvotes: 1

Views: 74

Answers (1)

Tom
Tom

Reputation: 9898

You can break it down and use VBA functions instead of Worksheet to make it a lot simple

Public Function NextName(A As Range, B As Range, C As Range) As String
    ' LEFT(TRIM(RIGHT(A2,LEN(A2)-LEN(B2)-LEN(C2)-2)),4)="and "
    If Left(Trim(Right(A.Value2, Len(A.Value2) - Len(B.Value2) - Len(C.Value2) - 2)), 4) = "and " Then
        ' RIGHT(TRIM(RIGHT(A17,LEN(A17)-LEN(B17)-LEN(C17)-2)),LEN(TRIM(RIGHT(A17,LEN(A17)-LEN(B17)-LEN(C17)-2)))-4)
        NextName = Right(Trim(Right(A.Value2, Len(A.Value2) - Len(B.Value2) - Len(C.Value2) - 2)), Len(Trim(Right(A.Value2, Len(A.Value2) - Len(B.Value2) - Len(C.Value2) - 2))) - 4)
    Else
        ' TRIM(RIGHT(A17,LEN(A17)-LEN(B17)-LEN(C17)-2))
        NextName = Trim(Right(A.Value2, Len(A.Value2) - Len(B.Value2) - Len(C.Value2) - 2))
    End If
End Function

I'd suspect you could make a lot of simplifications here as well but without example data I can't expand.

Having just seen your example data this is probably how I'd approach your scenario. You have no need for cells B or C doing it this way.

Public Function NextName(A As Range) As String
    Dim nmes As Variant, tmp As Variant
    Dim i As Long
    ' Split string into array on comma delimiter
    nmes = Split(A.Value2, ",")
    For i = LBound(nmes) To UBound(nmes)
        tmp = Empty
        ' Test if "and" in the string
        If InStr(nmes(i), " and ") Then
            ' Split string using " and " as delimiter
            tmp = Split(nmes(i), " and ")
            ' Return only last value
            nmes(i) = Trim(tmp(UBound(tmp)))
        ' This isn't really needed but I've left it in in case you wanted to process any strings that didn't have " and " in them.
        Else
            nmes(i) = Trim(nmes(i))
        End If
    Next i
    ' Return values        
    NextName = Join(nmes, ", ")
End Function

Upvotes: 2

Related Questions