如何将数字截断为前八位数字?

时间:2019-08-02 19:08:41

标签: excel vba

我正在处理一个宏,该宏会从8位数字的帐号末尾截断多余的数字。

我尝试使用不同类型的循环。

'Asks how many entries then inputs it into FOR loop
entryNum = InputBox("Number of Entries: ", "Account Number Truncator")

For counter = 2 To entryNum
    acctCor = Len(Cells(counter, 1))
    While acctCor > 8 'Loop to check the length of the number and then removes the last erroneous digits
        Cells(counter, 1).Value = Left(Cells(counter, 1), Len(Cells(counter, 1)) - 1) 'This is the part that's supposed to truncate the number
        acctCor = Len(Cells(counter, 1))
    Wend
Next counter

像547890012508973240987这样的数字在应该为54789001的情况下会转换为5478.900,并且似乎只适用于位数较小的数字。

1 个答案:

答案 0 :(得分:3)

您可以使用Left()而不是一一砍掉数字:

entryNum = InputBox("Number of Entries: ", "Account Number Truncator") 'Asks how many entries then inputs it into FOR loop
For counter = 2 To entryNum
    With Cells(counter, 1)
        .NumberFormat = "@"
        .Value = Left(.Value,8)
    End With
Next counter

此外-如果您不打算将数字用作实际数字(例如,它们只是标识符-您将不对它们进行任何数学运算),请务必小心使用Excel中的数字

例如547890012508973240987超出了Excel的15位精度的限制。

https://support.office.com/en-us/article/keeping-leading-zeros-and-large-numbers-1bf7b935-36e1-4985-842f-5dfa51f85fe7

https://docs.microsoft.com/en-us/office/troubleshoot/excel/long-numbers-incorrectly-in-excel

https://superuser.com/questions/413226/why-does-excel-treat-long-numeric-strings-as-scientific-notation-even-after-chan

相关问题