按公式解密数字(例如AABC)到数字(例如3328),提供密钥

时间:2015-10-11 06:34:09

标签: excel text numbers excel-formula

根据以下规则,我有一些价格代码,例如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$2:$D$3,$E$2:$E$3)

我可以使用任何简单的公式吗?

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

我想这并不简单,但至少是一种替代方案。文本到列固定宽度以分隔每个字符(ColumnsA:E)。音译的公式:

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

,复制到L1并向下复制以适应转换规则(P1:Q8)的表(名为Table),最后复制一些串联以重新组合组件:

=H1&I1&J1&K1&L1  
在N1中

并复制到适合:

SO3062243 example

答案 3 :(得分:0)

这两个用户定义函数(又名UDF)大致颠倒了this中答案提供的功能。

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

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