我在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] 删除。
我来说两句