如何在Excel中制作蒙版?

xxx参议员xxx

我在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")"但是有错误

罗恩·罗森菲尔德

由于您还在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左填充,而第三组用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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章