What is largest value (number) that I can store in an Excel VBA variable?
up vote
2
down vote
favorite
I am using Microsoft Office Excel 2007.
Suppose I have the number 12500000000000.
Dim value1 as long
value1 = CLng(copyrng2.value)
'where copyrng2 is a range (cell) variable containing 12500000000000
The above code will fail because of overflow.
Can I store that number in any Excel (VBA) variable? If not, what can I do to store it, preferably in a format (Integer, Long, etc.) on which calculations can be performed?
microsoft-excel microsoft-excel-2007 vba
add a comment |
up vote
2
down vote
favorite
I am using Microsoft Office Excel 2007.
Suppose I have the number 12500000000000.
Dim value1 as long
value1 = CLng(copyrng2.value)
'where copyrng2 is a range (cell) variable containing 12500000000000
The above code will fail because of overflow.
Can I store that number in any Excel (VBA) variable? If not, what can I do to store it, preferably in a format (Integer, Long, etc.) on which calculations can be performed?
microsoft-excel microsoft-excel-2007 vba
1
Large Number Arithmetic
– DavidPostill♦
Feb 16 '16 at 13:57
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am using Microsoft Office Excel 2007.
Suppose I have the number 12500000000000.
Dim value1 as long
value1 = CLng(copyrng2.value)
'where copyrng2 is a range (cell) variable containing 12500000000000
The above code will fail because of overflow.
Can I store that number in any Excel (VBA) variable? If not, what can I do to store it, preferably in a format (Integer, Long, etc.) on which calculations can be performed?
microsoft-excel microsoft-excel-2007 vba
I am using Microsoft Office Excel 2007.
Suppose I have the number 12500000000000.
Dim value1 as long
value1 = CLng(copyrng2.value)
'where copyrng2 is a range (cell) variable containing 12500000000000
The above code will fail because of overflow.
Can I store that number in any Excel (VBA) variable? If not, what can I do to store it, preferably in a format (Integer, Long, etc.) on which calculations can be performed?
microsoft-excel microsoft-excel-2007 vba
microsoft-excel microsoft-excel-2007 vba
edited Feb 16 '16 at 14:59
Ben N
28.8k1394139
28.8k1394139
asked Feb 16 '16 at 9:58
D.chan
42139
42139
1
Large Number Arithmetic
– DavidPostill♦
Feb 16 '16 at 13:57
add a comment |
1
Large Number Arithmetic
– DavidPostill♦
Feb 16 '16 at 13:57
1
1
Large Number Arithmetic
– DavidPostill♦
Feb 16 '16 at 13:57
Large Number Arithmetic
– DavidPostill♦
Feb 16 '16 at 13:57
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
The existing answer is correct in that you need to use the Double data type. That solves the problem, now I'll bring in some technical details.
The largest number that the Double data type can store (also the biggest number Excel can deal with) is 1.79769313486231570 • 10308. The biggest number that you can put in a cell (without a formula), however, is 9.99999999999999 • 10307. Note that you'll lose a good deal of precision when you're working with numbers that huge - adding a comparatively tiny number to things of that magnitude has a good chance of doing nothing. Floating point can get a little weird.
The maximum size of the Long data type is a drop in the ocean compared to Double's range; a signed 64-bit integer can only go up to 9,223,372,036,854,775,807. At least you're guaranteed to lose nothing to rounding there, as long as you're dealing with only whole numbers.
Sources: Excel specifications and limits, Data Type Summary
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
add a comment |
up vote
0
down vote
You need to use Double rather than Long. With B2 like:
This code will throw an error:
Sub dural()
Dim d As Long
d = Range("B2").Value
End Sub
But this code will not:
Sub BigNumber()
Dim d As Double
d = Range("B2").Value
MsgBox d
End Sub
add a comment |
up vote
0
down vote
Use the Decimal
variable type. You can't declare a number as Decimal
but you can store it in a Variant
variable.
Dim value1 as Variant
value1 = CDec(copyrng2.value)
NOTE: With the large numbers you are dealing with (12500000000000) if you try to store a decimal, Excel seems to round that number to 1 decimal place.
add a comment |
up vote
0
down vote
An excel can store only 15 digits. As far as the largest number be 999999999999999. If you try to type 16 digits number, it rounds the last number by 0 from 16th place.
New contributor
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The existing answer is correct in that you need to use the Double data type. That solves the problem, now I'll bring in some technical details.
The largest number that the Double data type can store (also the biggest number Excel can deal with) is 1.79769313486231570 • 10308. The biggest number that you can put in a cell (without a formula), however, is 9.99999999999999 • 10307. Note that you'll lose a good deal of precision when you're working with numbers that huge - adding a comparatively tiny number to things of that magnitude has a good chance of doing nothing. Floating point can get a little weird.
The maximum size of the Long data type is a drop in the ocean compared to Double's range; a signed 64-bit integer can only go up to 9,223,372,036,854,775,807. At least you're guaranteed to lose nothing to rounding there, as long as you're dealing with only whole numbers.
Sources: Excel specifications and limits, Data Type Summary
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
add a comment |
up vote
1
down vote
accepted
The existing answer is correct in that you need to use the Double data type. That solves the problem, now I'll bring in some technical details.
The largest number that the Double data type can store (also the biggest number Excel can deal with) is 1.79769313486231570 • 10308. The biggest number that you can put in a cell (without a formula), however, is 9.99999999999999 • 10307. Note that you'll lose a good deal of precision when you're working with numbers that huge - adding a comparatively tiny number to things of that magnitude has a good chance of doing nothing. Floating point can get a little weird.
The maximum size of the Long data type is a drop in the ocean compared to Double's range; a signed 64-bit integer can only go up to 9,223,372,036,854,775,807. At least you're guaranteed to lose nothing to rounding there, as long as you're dealing with only whole numbers.
Sources: Excel specifications and limits, Data Type Summary
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The existing answer is correct in that you need to use the Double data type. That solves the problem, now I'll bring in some technical details.
The largest number that the Double data type can store (also the biggest number Excel can deal with) is 1.79769313486231570 • 10308. The biggest number that you can put in a cell (without a formula), however, is 9.99999999999999 • 10307. Note that you'll lose a good deal of precision when you're working with numbers that huge - adding a comparatively tiny number to things of that magnitude has a good chance of doing nothing. Floating point can get a little weird.
The maximum size of the Long data type is a drop in the ocean compared to Double's range; a signed 64-bit integer can only go up to 9,223,372,036,854,775,807. At least you're guaranteed to lose nothing to rounding there, as long as you're dealing with only whole numbers.
Sources: Excel specifications and limits, Data Type Summary
The existing answer is correct in that you need to use the Double data type. That solves the problem, now I'll bring in some technical details.
The largest number that the Double data type can store (also the biggest number Excel can deal with) is 1.79769313486231570 • 10308. The biggest number that you can put in a cell (without a formula), however, is 9.99999999999999 • 10307. Note that you'll lose a good deal of precision when you're working with numbers that huge - adding a comparatively tiny number to things of that magnitude has a good chance of doing nothing. Floating point can get a little weird.
The maximum size of the Long data type is a drop in the ocean compared to Double's range; a signed 64-bit integer can only go up to 9,223,372,036,854,775,807. At least you're guaranteed to lose nothing to rounding there, as long as you're dealing with only whole numbers.
Sources: Excel specifications and limits, Data Type Summary
answered Feb 16 '16 at 14:56
Ben N
28.8k1394139
28.8k1394139
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
add a comment |
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
just one more question, will double support decimal places?
– D.chan
Feb 18 '16 at 1:05
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
@D.chan Yes, Double does; Long doesn't.
– Ben N
Feb 18 '16 at 3:57
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
If you need numbers, we got numbers
– Raystafarian
Feb 19 '16 at 17:23
add a comment |
up vote
0
down vote
You need to use Double rather than Long. With B2 like:
This code will throw an error:
Sub dural()
Dim d As Long
d = Range("B2").Value
End Sub
But this code will not:
Sub BigNumber()
Dim d As Double
d = Range("B2").Value
MsgBox d
End Sub
add a comment |
up vote
0
down vote
You need to use Double rather than Long. With B2 like:
This code will throw an error:
Sub dural()
Dim d As Long
d = Range("B2").Value
End Sub
But this code will not:
Sub BigNumber()
Dim d As Double
d = Range("B2").Value
MsgBox d
End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to use Double rather than Long. With B2 like:
This code will throw an error:
Sub dural()
Dim d As Long
d = Range("B2").Value
End Sub
But this code will not:
Sub BigNumber()
Dim d As Double
d = Range("B2").Value
MsgBox d
End Sub
You need to use Double rather than Long. With B2 like:
This code will throw an error:
Sub dural()
Dim d As Long
d = Range("B2").Value
End Sub
But this code will not:
Sub BigNumber()
Dim d As Double
d = Range("B2").Value
MsgBox d
End Sub
answered Feb 16 '16 at 14:33
Gary's Student
13.2k31729
13.2k31729
add a comment |
add a comment |
up vote
0
down vote
Use the Decimal
variable type. You can't declare a number as Decimal
but you can store it in a Variant
variable.
Dim value1 as Variant
value1 = CDec(copyrng2.value)
NOTE: With the large numbers you are dealing with (12500000000000) if you try to store a decimal, Excel seems to round that number to 1 decimal place.
add a comment |
up vote
0
down vote
Use the Decimal
variable type. You can't declare a number as Decimal
but you can store it in a Variant
variable.
Dim value1 as Variant
value1 = CDec(copyrng2.value)
NOTE: With the large numbers you are dealing with (12500000000000) if you try to store a decimal, Excel seems to round that number to 1 decimal place.
add a comment |
up vote
0
down vote
up vote
0
down vote
Use the Decimal
variable type. You can't declare a number as Decimal
but you can store it in a Variant
variable.
Dim value1 as Variant
value1 = CDec(copyrng2.value)
NOTE: With the large numbers you are dealing with (12500000000000) if you try to store a decimal, Excel seems to round that number to 1 decimal place.
Use the Decimal
variable type. You can't declare a number as Decimal
but you can store it in a Variant
variable.
Dim value1 as Variant
value1 = CDec(copyrng2.value)
NOTE: With the large numbers you are dealing with (12500000000000) if you try to store a decimal, Excel seems to round that number to 1 decimal place.
answered Jun 5 at 17:50
ChrisB
1366
1366
add a comment |
add a comment |
up vote
0
down vote
An excel can store only 15 digits. As far as the largest number be 999999999999999. If you try to type 16 digits number, it rounds the last number by 0 from 16th place.
New contributor
add a comment |
up vote
0
down vote
An excel can store only 15 digits. As far as the largest number be 999999999999999. If you try to type 16 digits number, it rounds the last number by 0 from 16th place.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
An excel can store only 15 digits. As far as the largest number be 999999999999999. If you try to type 16 digits number, it rounds the last number by 0 from 16th place.
New contributor
An excel can store only 15 digits. As far as the largest number be 999999999999999. If you try to type 16 digits number, it rounds the last number by 0 from 16th place.
New contributor
New contributor
answered 2 days ago
Raju Ghimire
1
1
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1041458%2fwhat-is-largest-value-number-that-i-can-store-in-an-excel-vba-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Large Number Arithmetic
– DavidPostill♦
Feb 16 '16 at 13:57