Data type Long is short for long integers are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647. Declare as Dim LngNum as Long or Dim LngNum&. Compare with integer, long is more bigger range and required more space in your computer memory.
For more example refer below table and code.
Below table we try to get balance stock after top up and deduction by
using different data type for example direct input, long and integer.
Base on result there is huge different in figure, long is still accurate but integer is totally wrong because the data is out of range for integer itself.
VBA Vode:
Sub Examples_Integer()
Dim i As Long
'Integer Declaration
Dim IntOpnStk As Integer, IntInOut As Integer
Dim IntClsStk%
'Decimal Declaration
Dim LngOpnStk As Long, LngInOut As Long
Dim LngClsStk&
i = 2
With ActiveSheet
Do
'Integer
On Error Resume Next
IntOpnStk = .Range("C" & i): IntInOut = .Range("D" & i)
IntClsStk% = IntOpnStk + IntInOut
'Long
LngOpnStk = .Range("C" & i): LngInOut = .Range("D" & i)
LngClsStk& = LngOpnStk + LngInOut
'Direct input
.Range("E" & i) = .Range("C" & i) + .Range("D" & i)
.Range("F" & i) = LngClsStk& 'Long variables
.Range("G" & i) = IntClsStk% 'Integer variables
IntOpnStk = 0: IntInOut = 0: IntClsStk% = 0
LngOpnStk = 0: LngInOut = 0: LngClsStk& = 0
i = i + 1
Loop Until .Range("A" & i) = ""
End With
End Sub
Note:
Please careful choose your data type unless your output is totally wrong after process, if you're lucky then error message will prompt for verification.
Microsoft Reference-Long-data-typeOther Reference-Long-data-type
Practice makes perfect. Thank You.
macro enabled excel
excel macro
vba coding
vba code
No comments:
Post a Comment