我在excel中有一个带有这些值的文本类型列
2/02/1472
22/88/1234
1/8/1234
22/88/12
01/01/222
88/2222
我想设置一个我的值看起来像这样的掩码
02/02/1472
22/88/1234
01/08/1234
22/88/1200
01/01/2220
00/88/2222
我的面具是00/00/0000(如果某个部分不存在,则填零)
我使用此"=text(A1,"00/00/0000")"
但有错误
答案 0 :(得分:4)
由于您还在标记中提到vba
,因此这是一个用户定义函数:
Option Explicit
Function FormatMask(S As String) As String
Dim V
Dim I As Long
V = Split(S, "/")
V(UBound(V)) = Format(V(UBound(V)), "0000")
For I = UBound(V) - 1 To 0 Step -1
V(I) = Format(V(I), "00")
Next I
FormatMask = Right("00/00/" & Join(V, "/"), 10)
End Function
@pnuts指出你的例子显示前两组用0' s左边填充,但是第三组右边用0填充。
以下修改实现了:
Option Explicit
Function FormatMask(S As String) As String
Dim V
Dim I As Long
V = Split(S, "/")
'This pads with 0's on the left
'V(UBound(V)) = Format(V(UBound(V)), "0000")
'For padding on right as you show for the last group only:
V(UBound(V)) = Left(V(UBound(V)) & "0000", 4)
For I = UBound(V) - 1 To 0 Step -1
V(I) = Format(V(I), "00")
Next I
FormatMask = Right("00/00/" & Join(V, "/"), 10)
End Function
答案 1 :(得分:3)
另一个例子,为什么电子表格软件不适合文本处理,但Excel可以使用(可怕的)公式进行管理:
=IF(LEN(IF(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))=1,"00/"&A1,IF(LEFT(RIGHT(A1,3))="/",A1&"00",IF(LEFT(RIGHT(A1,4))="/",A1&"0",IF(MID(A1,2,1)="/","0"&A1,A1)))))=10,IF(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))=1,"00/"&A1,IF(LEFT(RIGHT(A1,3))="/",A1&"00",IF(LEFT(RIGHT(A1,4))="/",A1&"0",IF(MID(A1,2,1)="/","0"&A1,A1)))),SUBSTITUTE(IF(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))=1,"00/"&A1,IF(LEFT(RIGHT(A1,3))="/",A1&"00",IF(LEFT(RIGHT(A1,4))="/",A1&"0",IF(MID(A1,2,1)="/","0"&A1,A1)))),"/","/0",1))
答案 2 :(得分:3)
对你来说另一个可怕的公式(我把它分解了所以我使用了一些你可以隐藏的辅助列,所以在视觉上它看起来一样)。部分可怕的原因还在于您的源数据没有一致的格式
在专栏A
中,我有原始列表,该列表以文本形式存储(Excel不会将这些列为日期或数字)
在列B
中获取第一部分我有公式
=IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2, VALUE(LEFT(A2,FIND("/",A2)-1)),0)
在列C
中获取中间位
=LEFT(RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))),FIND("/",RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))))-1)
然后在列D
中,我得到最后一位使用:
=RIGHT(A2,LEN(A2)-FIND("/",A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2, "/", ""))=2,FIND("/",A2)+1,1)))
然后我将它们放在列E
中并使用
=TEXT(B2,"00")&"/"&TEXT(C2, "00") &"/"&TEXT(D2,"0")&REPT(0,4-LEN(D2))
获取输出
你当然可以将这一切都合并到一个公式中,我只是将其分解为一点清晰(尽管仍然相当暗淡):
=TEXT(IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2, VALUE(LEFT(A2,FIND("/",A2)-1)),0),"00")&"/"&TEXT(LEFT(RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))),FIND("/",RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))))-1), "00") &"/"&TEXT(RIGHT(A2,LEN(A2)-FIND("/",A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2, "/", ""))=2,FIND("/",A2)+1,1))),"0")&REPT(0,4-LEN(RIGHT(A2,LEN(A2)-FIND("/",A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2, "/", ""))=2,FIND("/",A2)+1,1)))))
答案 3 :(得分:2)
略短的版本:
=IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=1,"00",TEXT(LEFT(A2,FIND("/",A2)-1),"00"))&"/"&TEXT(IFERROR(MID(A2,FIND("/",A2)+1,LOOKUP(99^99,FIND("/",A2,ROW($1:$20)))-FIND("/",A2)-1),TEXT(LEFT(A2,FIND("/",A2)-1),"00")),"00")&"/"&LEFT(RIGHT(A2,LEN(A2)-LOOKUP(99^99,FIND("/",A2,ROW($1:$20))))*10000,4)
您唯一想知道的是:
LOOKUP(99^99,FIND("/",A2,ROW($1:$20)))
这是查找/
的最后一次出现的函数。我假设字符串的最大长度为20,因此您可以根据需要替换它。