Excel在配方栏中显示前导零

我有一个没有0的数据列表,所以我试了一下
Format Cells->Custom
添加一个前导零,它的工作原理。

但是当我单击每个单元格时,公式栏中显示的数据仍然没有前导零。 例如,在自定义格式化后的excel文件中:
0112244555

但是在公式栏中:
112244555

有什么方法可以显示前导零的数据,当我点击每个人?

删除前导零是Excel中的默认行为。

使用自定义格式的解决方法是标准显示前导零

如果你想实际embedded他们,那么你将需要添加一个撇号,即在A1
'012
将显示
012

作为文本 – 虽然你仍然可以对这个单元格执行代数操作,就好像它是作为数字input一样
12

代码解决scheme

此代码将:

  • 仅在当前select中的数字常量单元格上运行(即忽略空格,文本,公式)
  • 将在撇号后面加上两个前导零

因此,如果您在下面的列A上运行代码,则结果将是列C中显示的更新单元格(仅供演示,实际更新发生在A1,A4和A5中)

在这里输入图像说明

改变这一行
strRep = "'00"
改变前导零的数量

'按Alt + F 11打开Visual Basic编辑器(VBE)
'从菜单中select插入模块。
'将代码粘贴到右侧的代码窗口中。
'按Alt + F11closuresVBE
'在Xl2003转到工具…macros…macros,然后双击AddLeadingZeros

 Sub AddLeadingZeros() Dim rng1 As Range Dim rngArea As Range Dim strRep As String Dim lngRow As Long Dim lngCol As Long Dim lngCalc As Long Dim X() strRep = "'00" On Error Resume Next 'Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8) Set rng1 = Selection.SpecialCells(xlConstants, xlNumbers) If rng1 Is Nothing Then Exit Sub On Error GoTo 0 'Speed up the code by turning off screenupdating and setting calculation to manual 'Disable any code events that may occur when writing to cells With Application lngCalc = .Calculation .ScreenUpdating = False .Calculation = xlCalculationManual .EnableEvents = False End With 'Test each area in the user selected range 'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on For Each rngArea In rng1.Areas 'The most common outcome is used for the True outcome to optimise code speed If rngArea.Cells.Count > 1 Then 'If there is more than once cell then set the variant array to the dimensions of the range area 'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks X = rngArea.Value2 For lngRow = 1 To rngArea.Rows.Count For lngCol = 1 To rngArea.Columns.Count 'replace the leading zeroes X(lngRow, lngCol) = strRep & X(lngRow, lngCol) Next lngCol Next lngRow 'Dump the updated array sans leading zeroes back over the initial range rngArea.Value2 = X Else 'caters for a single cell range area. No variant array required rngArea.Value = strRep & rngArea.Value2 End If Next rngArea 'cleanup the Application settings With Application .ScreenUpdating = True .Calculation = lngCalc .EnableEvents = True End With End Sub