【问题标题】:How to keep trailing zeroes in formatting decimal numbers?如何在格式化十进制数字时保持尾随零?
【发布时间】:2016-11-13 07:38:38
【问题描述】:

假设我在记事本中有以下数字:

1.19
0.040
10.1123
23.110
21.223
2.35456
4.0
10234.0009
456798.7500
123
34.560
40060
33.7876

如果我将数字复制并粘贴到 Excel 中,数字将变为:

1.19
0.04
10.1123
23.11
21.223
2.35456
4
10234.0009
456798.75
123
34.56
40060
33.7876

我想复制粘贴数字而不更改其原始格式,例如:

  • 0.040,不是 0.04
  • 4.0,不是 4
  • 456798.7500,不是 456798.75

我知道我可以使用条件格式,例如

If condition_1 Then
   Cells(...).NumberFormat = "0.00"

ElseIf condition_2 Then
   Cells(...).NumberFormat = "0.000"

...

Else
   result_else

End If

或者使用Select ... Case Statement,但是这些方法的问题是:

  1. 代码可能很长而且看起来很乱。
  2. 如果数字改变了,那么条件也必须改变。

所以我的问题是:

如何让Excel不截断十进制数末尾的零?

我想将值保留为数字,不是文本。

这种表示在财务数据中确实很重要,例如:外汇或货币汇率。

【问题讨论】:

  • 粘贴前需要将列格式改为Text。
  • 如果要使用条件格式,您会使用什么条件?
  • 为什么需要尾随的 0(它们似乎没有提供任何信息,如数字)?
  • @ScottHunter - 据我了解这个问题(我有一些先验知识)是为了证明应该以小数点后 4 位精度显示的货币总是以这种方式显示。不幸的是,其他货币可能只需要小数点后 2 位的精度。我认为关键是货币标识符。
  • Anastasiya - 如果您没有货币标识符列,那么这些数字无论如何都是没有意义的。为什么您如此不愿意透露这条非常重要的信息? (顺便说一句,投反对票的人可能不喜欢被告知数字作为文本是一个坏主意。有一个支持+1)

标签: vba excel formatting number-formatting


【解决方案1】:

如果有任何附带的列带有可用于将数值分组为货币格式类别的标识符,那么问题就变得简单了¹。


                       运行 currencyConditionals 子程序之前的数据

• EUR 应该使用 €,有 3 位小数并且是绿色的
• JPN 应该使用 ¥,有 4 位小数并且是深红色
• GPD 应使用 £,小数点后为 0,并且为深蓝色
• USD 应该使用 $,有 2 位小数并且是蓝色的

运行代码。

Option Explicit

Sub currencyConditionals()
    Dim a As Long, aCURRs As Variant, aFRMTS As Variant

    aCURRs = Array("EUR", "JPY", "GBP", "USD")
    aFRMTS = Array("[color10]_([$€-2]* #,##0.000_);[color3]_([$€-2]* (#,##0.000);[color15]_(* -??_);[color46]_(@_)", _
                   "[color9]_([$¥-411]* #,##0.0000_);[color9]_=[$¥-411]* (#,##0.0000);[color15]_(* -??_);[color46]_(@_)", _
                   "[color11]_([$£-809]* #,##0_-;[color11]-[$£-809]* #,##0_-;[color15]_-[$£-809]* -_);[color46]_-@_-", _
                   "[color5]_($* #,##0.00_);[color5]_($* (#,##0.00);[color15]_($* -??_);[color46]_(@_)")

    With Worksheets("Sheet2")
        With .Cells(1, 1).CurrentRegion
            With .Offset(1, 5).Resize(.Rows.Count - 1, 1)
                .FormatConditions.Delete
                For a = LBound(aCURRs) To UBound(aCURRs)
                    With .FormatConditions.Add(Type:=xlExpression, _
                                Formula1:="=$C2=" & Chr(34) & aCURRs(a) & Chr(34))
                        .NumberFormat = aFRMTS(a)
                    End With
                Next a
            End With
        End With
    End With
End Sub


                       运行 currencyConditionals 子程序后的数据


¹ 可以在Number Format Code找到格式化掩码代码的示例。

【讨论】:

  • 哇,这很有帮助。谢谢。 (+1)。顺便说一句,我现在正在考虑一个简单的方法。如果可行,我希望我能尽快发布答案
【解决方案2】:

如果我将您的原始数据放入列 A (使用您发布的格式) 并运行:

Sub CopyFull()
    Dim A As Range, B As Range

    Set A = Range("A1:A13")
    Set B = Range("B1:B13")
    A.Copy B
End Sub

复制的数据项将与原始数据项具有相同的格式:

因此,如果 A9 的 NumberFormat 为 0.0000,那么 B9 也是如此。

编辑#1:

如果数据是在打开的记事本进程中开始的,我会:

  1. 手动(或以编程方式)将数据存储为文本文件(.txt)
  2. 将文本文件作为文本导入一列
  3. 将列中的每一项转换为数字,NumberFormat与文本格式一致

【讨论】:

  • 看!终于有人回答了,而不是在不提供任何建议的情况下对我的 OP 投反对票。嗯,这真是一个有趣的答案。但是如果记事本中的数字,然后我想在将它们粘贴到 Excel 后保留原始格式?
  • 我使用您在 EDIT#1 中的建议来回答我自己的问题。谢谢。
  • 那不是说加里的学生回答了吗?
【解决方案3】:

虽然这段代码有点乱,但至少我尝试了 Gary 的学生在他的回答 (See EDIT#1) 中建议的内容。假设我想将原始数据放在 A 列,将所需格式放在 B 列,那么我会:

  1. 将 A 列中的数字格式设置为文本。为此,请单击 A 列 标题并查找 Home 菜单选项卡的 Number 部分 然后点击数字右下角的箭头 部分并选择文本。
  2. 复制(CtrlC)记事本中的原始数据,然后 单击单元格 A1 并粘贴 (CtrlV) 数据。

运行此代码:

Sub Keeping_Number_Format_1()
Last_Row = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To Last_Row
   'Determine the number of the decimal places
   Dec_Number = Len(Cells(i, 1)) - InStr(Cells(i, 1), ".")

   'Return the number found in the input string (each cells in column A)
   Cells(i, 2) = Val(Cells(i, 1))

   'Set the data in column B to specific formats
   If Dec_Number = Len(Cells(i, 1)) Then
      Cells(i, 2).NumberFormat = "0"
   ElseIf Dec_Number = 1 Then
      Cells(i, 2).NumberFormat = "0.0"
   ElseIf Dec_Number = 2 Then
      Cells(i, 2).NumberFormat = "0.00"
   ElseIf Dec_Number = 3 Then
      Cells(i, 2).NumberFormat = "0.000"
   ElseIf Dec_Number = 4 Then
      Cells(i, 2).NumberFormat = "0.0000"
   'I add these lines just in case the given numbers exceed 4 decimal places.
   'You can add the lines as you wish.
   ElseIf Dec_Number = 5 Then
      Cells(i, 2).NumberFormat = "0.00000"
   ElseIf Dec_Number = 6 Then
      Cells(i, 2).NumberFormat = "0.000000"
   End If
Next i
End Sub

或者可以运行上述代码的较短版本。它在 Microsoft Excel 中使用了REPT function,因此以后无需更改代码,因为它适用于任何小数位数的数据。

Sub Keeping_Number_Format_2()
Last_Row = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To Last_Row
   Dec_Number = Len(Cells(i, 1)) - InStr(Cells(i, 1), ".")
   Cells(i, 2) = Val(Cells(i, 1))

   If Dec_Number = Len(Cells(i, 1)) Then
      Cells(i, 2).NumberFormat = "0"
   Else
      Cells(i, 2).NumberFormat = "0." & WorksheetFunction.Rept("0", Dec_Number)
   End If

Next i
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2015-01-31
    • 2018-01-06
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多