从标题开始,我需要一个公式来在小数点后添加零。这需要保证有8个字符可供选择。
例如 7305.2
应该是 7305.002
.
需要它契约(Contract)引用 ID .我们的 ERP 要求所有契约(Contract)编号 ID 的长度为 8 个字符,并采用上面显示的格式。
最佳答案
考虑以下用户定义函数 (UDF):
Public Function PaddIt(r As Range) As String
Dim st As String, L As Long
st = r.Text
L = Len(st)
If L >= 8 Then
PaddIt = st
Exit Function
End If
If InStr(1, st, ".") = 0 Then
st = st & "."
If Len(st) = 8 Then
PaddIt = st
Exit Function
End If
End If
While Len(st) < 8
st = Replace(st, ".", ".0")
Wend
PaddIt = st
End Function
以下是一些示例输入/输出:
如果已经有8个字符,则返回输入。如果输入中没有“.”,则添加“.”。在“.”之后填充零,直到长度为8 .
用户定义函数 (UDF) 非常易于安装和使用:
ALT-M 打开一个新模块
如果您保存工作簿,UDF 将与它一起保存。
如果您使用的是 2003 年以后的 Excel 版本,则必须保存
文件为 .xlsm 而不是 .xlsx
要删除 UDF:
要使用 Excel 中的 UDF:
=PaddIt(A1)
要了解有关宏的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
有关 UDF 的详细信息,请参阅:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
必须启用宏才能使其工作!
关于excel - 公式在小数点后加 0 达到 8 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051538/