2

I need to find a way to put a 19-digit integer (scale of 9 × 10^18) in a Word document. The problem is it needs to run on a 32-bit machine so the LongLong data type will not work.

tried to split into a string array but that won't work either cause I need to do a calculation with it afterwards

How to fix this problem?

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Efratror
  • 21
  • 1
  • 3
  • Try `Double` instead of `Long`. – David Zemens Jan 14 '14 at 17:22
  • 1
    You'll need to handle the number as a string - VBA only support 15 digits of precision so anything more than that you'll lose some information. – Tim Williams Jan 14 '14 at 18:29
  • If you need to do calculations see here for example: http://tushar-mehta.com/misc_tutorials/project_euler/LargeNumberArithmetic.htm – Tim Williams Jan 14 '14 at 18:31
  • 1
    You are doing this wrong. An EAN code is a string, not a number. You use numbers for math, nobody ever multiplied an EAN code. – Hans Passant Jan 14 '14 at 19:54
  • I don't multiply the EAN code i use a calculation to get the last 5 digits (add-on) of the total code but I have found an example how to split a string in say the first 13 characters and the last 5 thanks for the tip – Efratror Jan 14 '14 at 21:43

2 Answers2

1

If you just need the last five digits and you need those to be a number, you can use code like this:

Dim sEan As String
Dim lLastFive As Long

sEan = "90000000000000012345"
lLastFive = CLng(Right$(sEan, 5))

Debug.Print lLastFive, TypeName(lLastFive)

And you should get

 12345        Long
Dick Kusleika
  • 31,152
  • 4
  • 48
  • 68
  • 1
    Thank you so much it was exactly the code that I needed even if the first of the last five digits is a 0 is isn't written I thank you gratefully – Efratror Jan 16 '14 at 21:59
0

A large number like the example you provide will fit in a Double type:

Sub Test()
Dim num As Double
Dim lon As Long

num = 9 * 10 ^ 18  '## This should work

lon = 9 * 10 ^ 18  '## This will raise an error.

End Sub
David Zemens
  • 51,213
  • 11
  • 70
  • 118
  • 1
    i'm not sure, it will pass `9E+18` instead of `900000000000...000` which I believe is what OP needs. – Kazimierz Jawor Jan 14 '14 at 17:56
  • 3
    It will "fit", but doesn't VBA only manage 15 digits of precision? So something with >15 digits may not be correctly represented. – Tim Williams Jan 14 '14 at 18:01
  • Tried it but not the solution that I needed code came back with this ouput (8.01673891020201E+17 for tis variable 801673891020201403 I need the full Number because it is an EAN code with 5 digit addon – Efratror Jan 14 '14 at 18:03
  • @Efratror Tim and Kazjaw are correct about the decimal precision allowed in VBA. See [THIS LINK](http://tushar-mehta.com/misc_tutorials/project_euler/LargeNumberArithmetic.htm) which may be of use to your particular problem. – David Zemens Jan 14 '14 at 18:29
  • I had found that same page but couldn't get it to work but now I'm going to use a string splitting methode [click here](http://www.vbaexpress.com/forum/showthread.php?39779-Splitting-Strings-Every-X-Characters) – Efratror Jan 14 '14 at 21:47
  • Right. It seems very complicated if you needed to perform arithmetic on such large numbers. String manipulation is likely to be your best bet, and can be easily done with regular string functions like `Left`, `Mid`, etc. – David Zemens Jan 14 '14 at 21:59