简体   繁体   English

使用 3 个单元的 UDF

[英]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.我正在尝试将以下公式转换为 UDF,主要是为了让人们更容易使用。

=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. A2 是一个名称列表,用逗号或“和”分隔,B 和 C2 是我们从字符串左侧删除的前一个名字和姓氏。

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.我已更改位以使用 Application.WorksheetFunction 并将单元格引用更改为 A、B 和 C.Value2 但我似乎无法让它工作。

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) (A2)

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

(B2) (B2)

John Smith Rogerson

(C2) (C2)

Rogerson

(Result) (结果)

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

You can break it down and use VBA functions instead of Worksheet to make it a lot simple您可以将其分解并使用VBA函数而不是Worksheet使其变得非常简单

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.您不需要单元格 B 或 C 这样做。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM