我想扩展下面的值。任何人都有一个很好的公式或流程,我想获取的格式是000.000.000.000,并且可能会出现或多或少的时期,例如000.000.000。
(起始)单元格A2
1.6.129.5
(公式)单元格B2
001.006.129.005
*我正在使用excel 2016
谢谢!
答案 0 :(得分:2)
如果您具有Office 365 Excel,则可以将TEXTJOIN用作数组公式:
=TEXTJOIN(".",TRUE,TEXT(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),(ROW($A$1:INDEX(A:A,LEN(A1)-LEN(SUBSTITUTE(A1,".",""))+1))-1)*99+1,99)),"000"))
作为数组公式,退出编辑模式时必须使用Ctrl-Shift-Enter而不是Enter进行确认。
如果没有Office 365,则需要分别解析每个部分:
=MID(
IF(ISNUMBER(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),1,99))),"." & TEXT(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),1,99)),"000"),"") &
IF(ISNUMBER(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),99,99))),"." & TEXT(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),99,99)),"000"),"") &
IF(ISNUMBER(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),198,99))),"." & TEXT(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),198,99)),"000"),"") &
IF(ISNUMBER(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),297,99))),"." & TEXT(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),297,99)),"000"),"") &
IF(ISNUMBER(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),396,99))),"." & TEXT(--TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",99)),396,99)),"000"),"")
,2,99)
您将需要添加尽可能多的部件的新IF,该部件假定部件的最大数量为5。
或者您可以使用此UDF,该UDF在提供的定界符上分割,然后使用提供的数字格式格式化每个部分,并将其放回原处:
Function MyFormat(str As String, delim As String, pat As String) As String
Dim strArr() As String
strArr = Split(str, delim)
Dim i As Long
For i = LBound(strArr) To UBound(strArr)
If IsNumeric(strArr(i)) Then
strArr(i) = Format(Val(strArr(i)), pat)
End If
Next i
MyFormat = Join(strArr, delim)
End Function
然后您将在工作表中使用它:
=MyFormat(A1,".","000")
答案 1 :(得分:0)
这是一个用户定义的函数,应注意任何长度。这应该粘贴到标准的公共模块代码表中(Alt + F11,Insert,Module)。
Option Explicit
Function paddZeroes(str As String, _
Optional padding As Long = 3)
Dim i As Long, tmp As Variant
tmp = Split(str, Chr(46))
For i = LBound(tmp) To UBound(tmp)
tmp(i) = Format(CLng(tmp(i)), String(padding, "0"))
Next i
paddZeroes = Join(tmp, Chr(46))
End Function