使用提供的密钥,通过公式将字母(例如 AABC)解密为数字(例如 3328)

Decipher letters (e.g. AABC) to numbers (e.g. 3328) by formula, with provided key

我有一些价格代码,例如BBAXVGGZTR,根据这些规则分别转换为数字2230044000

F > 1  
B > 2  
A > 3  
G > 4  
O > 5  
J > 6  
L > 7  
C > 8  
E > 9  
Else > 0

我目前正在使用 VBA:

Function SubstituteMultiple(text As String, old_text As Range, new_text As Range)
    Dim i As Single
    For i = 1 To old_text.Cells.Count
        Result = Replace(LCase(text), LCase(old_text.Cells(i)), LCase(new_text.Cells(i)))
        text = Result
    Next i
    SubstituteMultiple = Result
    End Function

例如:

=SubstituteMultiple(A2,$D:$D,$E:$E)

有没有我可以使用的简单公式?

如果价格代码在单元格 A2 中,请在单元格 B2 中输入此公式:

=SUMPRODUCT(IFERROR(SEARCH(MID(A2,{1,2,3,4,5},1),"fbagojlce"),0)*{10000,1000,100,10,1})

任意长度(在合理范围内)

=SUM(10^(LEN(A1)-ROW(INDIRECT(":$" & LEN(A1)))) *  
  IFERROR(SEARCH(MID(A1,ROW(INDIRECT(":$" & LEN(A1))),1),"FBAGOJLCE"),0))

Ctrl+Shift+Enter 因为它是一个数组公式。

我想不是更简单,但至少是一个替代方案。 Text to Columns 固定宽度来分隔每个字符(ColumnsA:E)。音译公式:

=IF(A1="","",IFERROR(VLOOKUP(A1,Table,2,0),"0"))  

在 H1 中,复制到 L1 并向下复制以适应转换规则 (P1:Q8) 的 table(命名为 Table),最后进行一些串联以重新组装组件:

=H1&I1&J1&K1&L1  

在 N1 中并向下复制以适应:

这两个User Defined Function(又名UDF)大致反转了中答案提供的功能。

Function alpha_to_num(str As String)
    Dim c As Long, tmp As String, vRPL As Variant

    vRPL = Array(3, 2, 8, 0, 9, 1, 4, 0, 0, 6, 0, 7, 0, 0, 5)
    tmp = UCase(str)

    For c = 1 To Len(tmp)
        Select Case Asc(Mid$(tmp, c, 1))
            Case 65, 66, 67, 69, 70, 71, 74, 76, 79
                tmp = Replace(tmp, Mid$(tmp, c, 1), vRPL(Asc(Mid$(tmp, c, 1)) - 65))
            Case Is > 57
                tmp = Replace(tmp, Mid$(tmp, c, 1), 0)
            Case Else
                'do nothing
        End Select
    Next c

    alpha_to_num = tmp

End Function

Function alpha_to_num_two(str As String)
    Dim c As Long, tmp As String

    tmp = "fbagojlce"

    For c = 1 To Len(str)
        If Asc(Mid$(str, c, 1)) > 57 Then _
            str = Replace$(str, Mid$(str, c, 1), InStr(1, tmp, Mid$(str, c, 1), vbTextCompare))
    Next c

    alpha_to_num_two = str

End Function

两者都具有不覆盖替换的优点。