【发布时间】:2011-11-02 09:10:38
【问题描述】:
VBA 中是否有办法以编程方式获取数值类型(例如Long)的限制(最小值、最大值)?
类似于 C++ 中的 numeric_limits<long>::min()。
【问题讨论】:
标签: vba
VBA 中是否有办法以编程方式获取数值类型(例如Long)的限制(最小值、最大值)?
类似于 C++ 中的 numeric_limits<long>::min()。
【问题讨论】:
标签: vba
不,但无论如何它们都是固定大小的,因此您可以直接推断它们。
这里有一些关于它们尺寸的信息:http://msdn.microsoft.com/en-us/library/aa164754.aspx
来自文章:
Integer 和 Long 数据类型都可以保存正值或负值。它们之间的区别在于它们的大小:整数变量的值可以在 -32,768 到 32,767 之间,而长变量的值可以在 -2,147,483,648 到 2,147,483,647 之间。传统上,VBA 程序员使用整数来保存小数字,因为它们需要更少的内存。然而,在最近的版本中,VBA 将所有整数值转换为 Long 类型,即使它们被声明为 Integer 类型。因此,使用整数变量不再具有性能优势;事实上,Long 变量可能会稍微快一些,因为 VBA 不必转换它们。
【讨论】:
min=-2^((LenB(value)*8)-1) 和 max=(((2^((LenB(value)*8)-2))-1)*2)+1 计算它们的范围.后者中更复杂的数学是避免溢出 - 即对于 32 位有符号 int 计算 2^30,减 1,乘以 2,加 1,得到(2^30-1)*2+1 = 2^31-1
我不认为有这样的功能。我会为每种数字类型创建一个 const 值库,然后您可以引用它。
【讨论】:
对于具有 32 位关节的编程平台:“Dim Item1 As Long”,变量长度为 32 位。这意味着每个 Long 变暗变量都是 32 位的。它可以包含的最大值(正或负)略高于 20 亿。
Sub sumall()
Dim firstRow As long
firstRow = 5
Dim lastRow Aslong
lastRow = 12
Dim aRow As long
Dim sumall As Variant
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 = Cells(aRow, 2).Value
previousValue = Cells(aRow, 2)
End If
Next aRow
sumall = sumResult
End Sub
任务的另一个选项是使用 scriptingDictionary 仅获取唯一值:
Sub sumall()
Dim objDictionary As Object
Dim firstRow As Long
firstRow = 5
Dim lastRow As Long
lastRow = 12
Dim aRow As Variant
Dim varKey As Variant
Dim sumResult As Variant
Set objDictionary = CreateObject("Scripting.Dictionary")
For aRow = firstRow To lastRow
If objDictionary.exists(Cells(aRow, 2).Value) = False Then
objDictionary.Add Cells(aRow, 2).Value, True
End If
Next aRow
sumResult = 0
For Each varKey In objDictionary.keys
sumResult = varKey + sumResult
Next varKey
End Sub
【讨论】:
Sub highlong()
Dim x As Long
On Error GoTo Prt
Do While True
x = x + 1
Loop
Prt:
MsgBox (x)
End Sub
随便你的船。
【讨论】: