用于用散文格式描述的代码中替换字母数字数字的公式

时间:2018-09-28 14:09:12

标签: excel formula

我需要找到一个用代码的每一位代替描述的公式,并使它显示为串联文本。

例如:

“ 134B2”-“年龄更大,身材更高,更富有,身体不好,男孩”

“ 1324B”-“年龄较大,身高较高,男孩,较富有,较差”

1 个答案:

答案 0 :(得分:0)

这是您可以使用的用户定义功能。

Arg1 stringToDecode要转换的字符串。您还可以传递包含字符串的范围地址

Arg2 keys这是密码的密钥部分。必须以逗号分隔的列表形式传递。

Arg2 values这是密码的相应值部分。必须以逗号分隔的列表形式传递。

Option Explicit
Public Sub test()
    Debug.Print GetCodedOutput([A1], "1,3,4,B,2", "Older,Tall,Richer,Bad,Boy")
End Sub

Public Function GetCodedOutput(ByVal stringToDecode As String, ByVal keys As String, ByVal values As String)
    Dim arr1() As String, arr2()  As String, dict As Object, i As Long, outputString As String
    Set dict = CreateObject("Scripting.Dictionary")
    arr1 = Split(keys, ",")
    arr2 = Split(values, ",")

    If UBound(arr1) <> UBound(arr2) Or stringToDecode = vbNullString Then
        GetCodedOutput = CVErr(xlErrNA)
        Exit Function
    End If

    For i = LBound(arr1) To UBound(arr1)
        dict(arr1(i)) = arr2(i)
    Next i

    Dim outputArr(), upper As Long
    upper = Len(stringToDecode)
    ReDim outputArr(1 To upper)
    For i = 1 To upper
        On Error GoTo errhand
        outputArr(i) = dict(Mid$(stringToDecode, i, 1))
    Next
    GetCodedOutput = Join$(outputArr, ",")
    Exit Function
errhand:
    GetCodedOutput = CVErr(xlErrNA)
    Exit Function
End Function

工作表中的UDF:

enter image description here