无法将string转换为VBA中的整数或Excel公式(即使string都是数字,也无法转换或input错误的数字)

我有一个单元格内的string:105501008962100001

单元格A10包含此文本。

当我运行ISTEXT(A10)时,它返回TRUE。

我使用了VALUE(A10),但返回105501008962100000

当我乘以A10 * 1时,我也得到了105501008962100000。

我也创build了一个VBA函数来转换为整数,但是当我创build检查点时,它创build一个错误,说有一个失败转换为整数。

我不明白为什么当它完全由数字组成时,我不能将这个单元格内的string转换为一个整数。 此号码是从CSV文件导入的。

Function ConvertToInteger(v1 As Variant) As Integer On Error GoTo 100: 'MsgBox Len(v1) Dim tempArray As Variant Dim arraySize As Integer arraySize = (Len(v1) - 1) ReDim tempArray(arraySize) Dim tempText As String Dim finalNumber As Integer Dim i As Integer For i = 1 To Len(v1) tempArray(i - 1) = CStr(Mid(v1, i, 1)) Next tempText = Join(tempArray, "") tempText = CStr(tempText) 'MsgBox tempText finalNumber = CInt(tempText) MsgBox finalNumber 'ConvertToInteger = finalNumber Exit Function 100: MsgBox "Failed to convert """ & v1 & """ to an integer.", , "Aborting - Failed Conversion" End End Function 

我对代码进行了修改,但是仍然在数字的末尾不是1:

 Function ConvertToInteger(v1 As Variant) As Double Dim tempArray As Variant Dim arraySize As Double arraySize = (Len(v1) - 1) ReDim tempArray(arraySize) Dim tempText As String Dim finalNumber As Double Dim i As Integer For i = 1 To Len(v1) tempArray(i - 1) = CStr(Mid(v1, i, 1)) Next tempText = Join(tempArray, "") tempText = CStr(tempText) finalNumber = CDbl(tempText) MsgBox finalNumber 'I get an error here when assigning finalNumber to ConvertToInteger 'ConvertToInteger = finalNumber End Function 

您可以使用内部Err对象的Description属性来识别error handling块中的此错误(溢出):

 100: MsgBox "Failed to convert """ & v1 & """ to an integer." & vbCrLf & Err.Description, , "Aborting - Failed Conversion" End 

正如你已经实现的那样,error handling程序可以工作 ,但是不会显示用于debugging问题的有用信息。

一旦确定为溢出错误,请注意,值105501008962100001超过了“整数”或“长”数据types所允许的大小。 声明它为一个Double而不是。

 Dim tempArray As Variant Dim arraySize As Double arraySize = (Len(v1) - 1) ReDim tempArray(arraySize) 

在Excel中使用大数字的注意限制。 你的数据可能会超过这个。

要处理非常大的数字,请参阅:

在VBA中处理大于Long的数字

随后:

http://tushar-mehta.com/misc_tutorials/project_euler/LargeNumberArithmetic.htm

以下是添加两个大数字的简单示例。 你将能够使用cLarge类对这些“数字”(实际上是string)进行算术运算。 您的函数将需要将types声明从Long/Double更改为String

在标准模块中,执行:

 Sub foo() Dim lrg As New cLarge MsgBox lrg.LargeAdd("105501008962100001", "205501231962100003") End Sub 

创build一个名为cLarge的类模块,并在该模块中放置以下代码:

 '### Class module for adding very large (> Decimal precision) values ' http://tushar-mehta.com/misc_tutorials/project_euler/LargeNumberArithmetic.htm ' ' Modified by David Zemens, 9 December 2015 Option Explicit Public cDecMax As Variant, cDecMaxLen As Integer, cSqrDecMaxLen As Integer Private pVal As String Public Sub Class_Initialize() Static Initialized As Boolean If Initialized Then Exit Sub Initialized = True cDecMax = _ CDec(Replace("79,228,162,514,264,337,593,543,950,335", ",", "")) 'this is 2^96-1, the limit on Decimal precision data in Excel/VBA cDecMaxLen = Len(cDecMax) - 1 cSqrDecMaxLen = cDecMaxLen \ 2 End Sub Function Ceil(X As Single) As Long If X < 0 Then Ceil = Fix(X) Else Ceil = -Int(-X) End Function Private Function addByParts(ByVal Nbr1 As String, ByVal Nbr2 As String) _ As String Dim NbrChunks As Integer If Len(Nbr1) > Len(Nbr2) Then _ Nbr2 = String(Len(Nbr1) - Len(Nbr2), "0") & Nbr2 _ Else _ Nbr1 = String(Len(Nbr2) - Len(Nbr1), "0") & Nbr1 NbrChunks = Ceil(Len(Nbr1) / cDecMaxLen) Dim I As Integer, OverflowDigit As String, Rslt As String OverflowDigit = "0" For I = NbrChunks - 1 To 0 Step -1 Dim Nbr1Part As String Nbr1Part = Mid(Nbr1, I * cDecMaxLen + 1, cDecMaxLen) Rslt = CStr(CDec(Nbr1Part) _ + CDec(Mid(Nbr2, I * cDecMaxLen + 1, cDecMaxLen)) _ + CDec(OverflowDigit)) If Len(Rslt) < Len(Nbr1Part) Then Rslt = String(Len(Nbr1Part) - Len(Rslt), "0") & Rslt OverflowDigit = "0" ElseIf I = 0 Then ElseIf Len(Rslt) > Len(Nbr1Part) Then OverflowDigit = Left(Rslt, 1): Rslt = Right(Rslt, Len(Rslt) - 1) Else OverflowDigit = "0" End If addByParts = Rslt & addByParts Next I End Function Function LargeAdd(ByVal Nbr1 As String, ByVal Nbr2 As String) As String 'Initialize If Len(Nbr1) <= cDecMaxLen And Len(Nbr2) <= cDecMaxLen Then LargeAdd = CStr(CDec(Nbr1) + CDec(Nbr2)) Exit Function End If If Len(Nbr1) > cDecMaxLen Then LargeAdd = addByParts(Nbr1, Nbr2) _ Else LargeAdd = addByParts(Nbr2, Nbr1) End Function 

所以,长话短说,你不能实际使用这些“数字”,你严格不能“转换”为整数/长整型值。 您需要使用自定义类实现来针对string值执行math运算。