在Excel中将条件零添加到ssn样式编号

时间:2017-03-03 19:39:47

标签: excel string

尝试在Excel中为ssn样式编号添加条件零。

1234-1234-12 -> 12340-1234-12
12345-123-12 -> 12345-1230-12
12345-1234-1 -> 12345-1234-10
12345-1234-12 -> do nothing

2 个答案:

答案 0 :(得分:2)

您需要解析每个部分,然后确保它是所需数量的数字并将它连接在一起:

=LEFT(LEFT(A1,FIND("-",A1)-1)&"00000",5)&"-" & LEFT(MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1)&"0000",4)&"-"&LEFT(MID(A1,FIND("-",A1,FIND("-",A1)+1)+1,LEN(A1))&"00",2)

enter image description here

答案 1 :(得分:1)

如果你想要一个VBA答案来补充@ scottcraner的优秀答案:

Function ExpandSSN(s As String) As String
    Dim A As Variant
    Dim part As String
    A = Split(s, "-")
    part = Trim(A(0))
    A(0) = part & String(5 - Len(part), "0")
    part = Trim(A(1))
    A(1) = part & String(4 - Len(part), "0")
    part = Trim(A(2))
    A(2) = part & String(2 - Len(part), "0")
    ExpandSSN = Join(A, "-")
End Function

将上述代码放在标准代码模块中。然后输入公式,例如B1中的=ExpandSSN(A1)并向下拖动:

enter image description here