Data type integer stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767. One byte is used to represent the sign either positive (+ve) or negative (-ve). Can be declare Dim IntNum as integer or Dim IntNum%. If assigned data is out of range then error "Run time error '6': Over flow.
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, integer and long.
Base on result there is no different in figure, but long is better unless we are sure the quantity is not out of range.
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
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) = IntClsStk% 'Integer variables
.Range("G" & i) = LngClsStk& 'Long 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:
Number of Maximum rows in Excel is 1,048,576 and Maximum columns is 16,384. Therefore loop through used range in rows must used long and columns as integer enough.
Other Reference-Integer-data-type
Practice makes perfect. Thank You.
excelmacros
macro excel
excel programming
excel vba
No comments:
Post a Comment