[英]How to restrict a cell to input only numbers
我有一个excel文件,验证一列只输入数字,但格式化为数字小于18,将添加前导零。 但现在第15位后的数字将转换为0 ex:“002130021300020789”,九将更改为0.将列转换为文本后,它接受但我无法添加前导零并且无法限制输入onlu数字。
感谢任何帮助..提前感谢。
MS文章 :Excel遵循IEEE 754规范关于如何存储和计算浮点数。 因此,Excel仅存储一个数字中的15位有效数字,并将第十五位后的数字更改为零。
要获得数字格式并确保用户只输入数字,您可以执行此操作。 我假设您正在验证范围A1中的文本。 请根据情况修改。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Intersect(Target, Range("A1")) Is Nothing Then
'~~> If entered text is not a number then erase input
If Not IsNumeric(Range("A1").Value) Then
MsgBox "invalid Input"
Application.Undo
GoTo LetsContinue
End If
Range("A1").Value = "'" & Format(Range("A1").Value, "000000000000000000")
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
跟进
如果要复制和粘贴,则必须先将范围G11:G65536手动格式化为TEXT ,然后使用此代码
SNAPSHOT(粘贴数值时)
SNAPSHOT(粘贴非数字值时)
码
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Dim cl As Range
Application.EnableEvents = False
If Not Intersect(Target, Range("G11:G" & Rows.Count)) Is Nothing Then
For Each cl In Target.Cells
'~~> If entered text is not a number then erase input
If Not IsNumeric(cl.Value) Then
MsgBox "invalid Input"
Application.Undo
GoTo LetsContinue
End If
cl.Value = "'" & Format(cl.Value, "000000000000000000")
Next
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
首先,您可以将单元格的格式更改为文本
Range("A1").NumberFormat = "@"
然后你可以添加零
cellvalue2 = Format(cellvalue1, "000000000000000") // for numeric fields
or
= TEXT(cellvalue1,"000000000000000") // for textfields
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.