为什么长数字types似乎是不够的:#VALUE! 错误

我有我写这个代码得到一个没有重复值的总和。 我有一个1424行和2列(string和值)的表。 所以我看看以前的值,如果当前值不一样,我总结一下。

Function sumAll() Dim firstRow As Long firstRow = 5 Dim lastRow As Long lastRow = 12 Dim aRow As Long Dim sumResult As Long sumResult = 0 Dim previousValue As Long previousValue = -1 For aRow = firstRow To lastRow If Cells(aRow, 2).Value <> previousValue Then sumResult = sumResult + Cells(aRow, 2) previousValue = Cells(aRow, 2) End If Next aRow sumAll = sumResult End Function 

但问题出现时,使一个lastRow大到30 – 我得到一个#VALUE! 错误。 但为什么? sumResult可能达到的最大值是60 912 997 662Long应该保持9,223,372,036,854,775,807 。

编辑:好的,我改变了Long Double ,它仍然给我的错误:

 Function sumAll() Dim firstRow As Long firstRow = 5 Dim lastRow As Long lastRow = 600 Dim aRow As Long Dim totalDoubles As Long totalDoubles = 0 Dim sumResult As Double sumResult = 0 Dim previousValue As Double previousValue = -1 For aRow = firstRow To lastRow If Cells(aRow, 2).Value <> previousValue Then sumResult = sumResult + Cells(aRow, 2) previousValue = Cells(aRow, 2) Else totalDoubles = totalDoubles + 1 End If Next aRow sumAll = sumResult MsgBox ("end: " & totalDoubles) End Function 

这适用于我使用双重或变体。 长整数的大小只有4个字节。 范围是-2,147,483,648到2,147,486,647。 Double是8个字节,Variant是16个字节。 这对我来说是完美的:

 Function sumAll() As Variant Dim firstRow As Integer: firstRow = 5 Dim lastRow As Integer: lastRow = 30 Dim aRow As Integer Dim sumResult As Variant: sumResult = 0 Dim previousValue As Variant: previousValue = -1 For aRow = firstRow To lastRow If Cells(aRow, 2).Value <> previousValue Then sumResult = sumResult + Cells(aRow, 2) previousValue = Cells(aRow, 2) End If Next aRow sumAll = sumResult End Function 

在这里你可以看到VBA数据types的范围。