在小数位后添加0以达到8个字符的公式

时间:2021-05-16 17:07:00

As from the title I need a formula to add zeros after the decimal place. This needs to ensure that there are 8 characters for selection.

从标题开始我需要一个公式来在小数位后添加零。这需要确保有8个字符供选择。

For example 7305.2 would need to be 7305.002.

例如7305.2需要7305.002。

It's required for a contract reference ID. Our ERP requires all contract number IDs to be 8 characters long and in the format displayed above.

它是合同参考ID所必需的。我们的ERP要求所有合同号码ID长度为8个字符,格式如上所示。

2 个解决方案

#1


Consider the following User Defined Function (UDF):

请考虑以下用户定义函数(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

Here are some sample inputs/outputs:

以下是一些示例输入/输出:

在小数位后添加0以达到8个字符的公式

If there are already 8 characters, the input is returned.
If there is no "." in the input, then a "." is added.
Zeros are padded after the "." until the length is 8.

如果已有8个字符,则返回输入。如果没有“。”在输入中,然后是“。”被添加。在“。”之后填充零。直到长度为8。

User Defined Functions (UDFs) are very easy to install and use:

用户定义函数(UDF)非常易于安装和使用:

  1. ALT-F11 brings up the VBE window
  2. ALT-F11调出VBE窗口

  3. ALT-I ALT-M opens a fresh module
  4. ALT-I ALT-M打开一个新模块

  5. paste the stuff in and close the VBE window
  6. 粘贴内容并关闭VBE窗口

If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

如果保存工作簿,UDF将随之保存。如果您在2003年之后使用的是Excel版本,则必须将文件另存为.xlsm而不是.xlsx

To remove the UDF:

要删除UDF:

  1. bring up the VBE window as above
  2. 如上所述调出VBE窗口

  3. clear the code out
  4. 清除代码

  5. close the VBE window
  6. 关闭VBE窗口

To use the UDF from Excel:

要从Excel使用UDF:

=PaddIt(A1)

To learn more about macros in general, see:

要了解有关宏的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

有关UDF的详细信息,请参阅:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

必须启用宏才能使其正常工作!

#2


Here is formula solution:

这是公式解决方案:

=IF(ISERROR(FIND(".",A1)),LEFT(A1&"."&"00000000",8),IF(LEN(A1)<8,LEFT(A1,FIND(".",A1)-1)&"."&RIGHT("00000000"&MID(A1,FIND(".",A1)+1,99),7-LEN(LEFT(A1,FIND(".",A1)-1))),A1))

在小数位后添加0以达到8个字符的公式

EDIT: link to Excel fomula beautifier

编辑:链接到Excel fomula beautifier

#1


Consider the following User Defined Function (UDF):

请考虑以下用户定义函数(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

Here are some sample inputs/outputs:

以下是一些示例输入/输出:

在小数位后添加0以达到8个字符的公式

If there are already 8 characters, the input is returned.
If there is no "." in the input, then a "." is added.
Zeros are padded after the "." until the length is 8.

如果已有8个字符,则返回输入。如果没有“。”在输入中,然后是“。”被添加。在“。”之后填充零。直到长度为8。

User Defined Functions (UDFs) are very easy to install and use:

用户定义函数(UDF)非常易于安装和使用:

  1. ALT-F11 brings up the VBE window
  2. ALT-F11调出VBE窗口

  3. ALT-I ALT-M opens a fresh module
  4. ALT-I ALT-M打开一个新模块

  5. paste the stuff in and close the VBE window
  6. 粘贴内容并关闭VBE窗口

If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

如果保存工作簿,UDF将随之保存。如果您在2003年之后使用的是Excel版本,则必须将文件另存为.xlsm而不是.xlsx

To remove the UDF:

要删除UDF:

  1. bring up the VBE window as above
  2. 如上所述调出VBE窗口

  3. clear the code out
  4. 清除代码

  5. close the VBE window
  6. 关闭VBE窗口

To use the UDF from Excel:

要从Excel使用UDF:

=PaddIt(A1)

To learn more about macros in general, see:

要了解有关宏的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

有关UDF的详细信息,请参阅:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

必须启用宏才能使其正常工作!

#2


Here is formula solution:

这是公式解决方案:

=IF(ISERROR(FIND(".",A1)),LEFT(A1&"."&"00000000",8),IF(LEN(A1)<8,LEFT(A1,FIND(".",A1)-1)&"."&RIGHT("00000000"&MID(A1,FIND(".",A1)+1,99),7-LEN(LEFT(A1,FIND(".",A1)-1))),A1))

在小数位后添加0以达到8个字符的公式

EDIT: link to Excel fomula beautifier

编辑:链接到Excel fomula beautifier