Is there a way in VBA to programmatically get the limits (minimum value, maximum value) of a numeric type (Long
for instance) ?
Something like the numeric_limits<long>::min()
in C++.
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
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.
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.
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.
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:
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
Whatever floats your boat.