VBA:以编程方式获取长限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7977891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:22:54  来源:igfitidea点击:

VBA : get Long limits programmatically

vba

提问by Jér?me

Is there a way in VBA to programmatically get the limits (minimum value, maximum value) of a numeric type (Longfor instance) ?

VBA 中有没有办法以编程方式获取数字类型(Long例如)的限制(最小值、最大值)?

Something like the numeric_limits<long>::min()in C++.

类似于numeric_limits<long>::min()C++ 中的。

回答by Polynomial

No, but they're fixed size anyway, so you can infer them directly.

不,但无论如何它们都是固定大小的,因此您可以直接推断它们。

Here's some info on their sizes: http://msdn.microsoft.com/en-us/library/aa164754.aspx

以下是有关它们大小的一些信息:http: //msdn.microsoft.com/en-us/library/aa164754.aspx

From the article:

从文章:

The Integer and Long data types can both hold positive or negative values. The difference between them is their size: Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647. Traditionally, VBA programmers have used integers to hold small numbers, because they required less memory. In recent versions, however, VBA converts all integer values to type Long, even if they are declared as type Integer. Therefore, there is no longer a performance advantage to using Integer variables; in fact, Long variables might be slightly faster because VBA does not have to convert them.

Integer 和 Long 数据类型都可以包含正值或负值。它们之间的区别在于它们的大小:整数变量可以保存 -32,768 到 32,767 之间的值,而 Long 变量的范围可以从 -2,147,483,648 到 2,147,483,647。传统上,VBA 程序员使用整数来保存小数,因为它们需要较少的内存。然而,在最近的版本中,VBA 将所有整数值转换为 Long 类型,即使它们被声明为 Integer 类型。因此,使用整数变量不再有性能优势;事实上,Long 变量可能会稍微快一点,因为 VBA 不必转换它们。

回答by Ash Burlaczenko

I don't think there's a function for that. I would create a library of const values for each number type then you could reference this.

我不认为有一个功能。我将为每个数字类型创建一个 const 值库,然后您可以参考它。

回答by N.Russ

For a programming platform with 32-Bit articuture: "Dim Item1 As Long", variable is 32-Bit in length. This means that each Long dimmed variable is 32-Bit. he maximum value it can contain (positive or negative) is a little over 2 billion.

对于具有 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

Another option for the tasc would be to use scriptingDictionary to get unique values only:

tasc 的另一个选择是使用 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

回答by user3819867

Sub highlong()
Dim x As Long
On Error GoTo Prt
Do While True
   x = x + 1
Loop
Prt:
   MsgBox (x)
End Sub

Whatever floats your boat.

无论什么漂浮在你的船上。