用科学数字将单元格内容修剪到一定长度
Posted
技术标签:
【中文标题】用科学数字将单元格内容修剪到一定长度【英文标题】:Trim Cell Contents to Certain Lenght with Scientific Numbers 【发布时间】:2017-07-25 16:27:24 【问题描述】:我有一列数字需要保留为数字,但长度限制为 20 个字符。
Sub casecheck()
Dim myString As String
Dim newString As String
Dim char As Variant
'replace all special characters with nothing
Const SpecialCharacters As String = "!,.,#,$,%,^,&,-,(,),,[,],,/,\, "
For i = 2 To trans
myString = Worksheets("Data Input").Cells(i, 10).Value
newString = myString
For Each char In Split(SpecialCharacters, ",")
newString = Replace(newString, char, "")
Worksheets("ExpertPay Feed").Cells(i, 3).Value = newString
Worksheets("ExpertPay Feed").Cells(i, 3).NumberFormat = "0"
Next
Next i
'trim length down to 20 characters
For i = 2 To trans
Worksheets("ExpertPay Feed").Cells(i, 3).Value = Left(Worksheets("ExpertPay Feed").Cells(i, 3).Value, 20)
Next i
End Sub
我的代码运行良好,除非我的数字很长。例如:
这个原数:1.31137E+17变成13113690920150400000000000000000 而且我似乎无法删除所有额外的零!
【问题讨论】:
【参考方案1】:试试这个功能:
Function TrimTrailingZeroes(BigNumber As String) As String
Dim i As Integer
For i = Len(BigNumber) To 1 Step -1
If Mid(BigNumber, i, 1) <> "0" Then
TrimTrailingZeroes = Left(BigNumber, i)
Exit Function
End If
Next i
TrimTrailingZeroes = BigNumber
End Function
有时 in 可能是将数字视为文本的一种简单方法。
【讨论】:
感谢您的想法,但它仍然无法正常工作。我得到了同样的东西:(以上是关于用科学数字将单元格内容修剪到一定长度的主要内容,如果未能解决你的问题,请参考以下文章