By Gareth

2014-10-16 16:11:21 8 Comments

I often see questions relating to Overflow errors with .

My question is why use the integer variable declaration instead of just defining all numerical variables (excluding double etc.) as long?

Unless you're performing an operation like in a for loop where you can guarantee that the value won't exceed the 32,767 limit, is there an impact on performance or something else that would dictate not using long?


@Patrick 2014-10-16 17:12:15

As noted in other answers, the real difference between int and long is the size of its memory space and therefore the size of the number it can hold.

here is the full documentation on these datatypes

an Integer is 16 bits and can represent a value between -32,768 and 32,767

a Long is 32 bits and can represent -2,147,483,648 to 2,147,483,647

and there is a LongLong which is 64 bits and can handle like 9 pentilion

One of the most important things to remember on this is that datatypes differ by both language and operating system / platform. In your world of VBA a long is 32 bits, but in c# on a 64 bit processor a long is 64 bits. This can introduce significant confusion.

Although VBA does not have support for it, when you move to any other language in .net or java or other, I much prefer to use the system datatypes of int16, int32 and int64 which allows me to b much more transparent about the values that can be held in these datatypes.

@PGCodeRider 2018-08-04 19:40:33

Even though this post is four years old, I was curious about this and ran some tests. The most important thing to note is that a coder should ALWAYS declare a variable as SOMETHING. Undeclared variables clearly performed the worst (undeclared are technically Variant)

Long did perform the fastest, so I have to think that Microsoft's recommendation to always use Long instead of Integer makes sense. I'm guessing the same as true with Byte, but most coders don't use this.


Variable Olympics

Code Used:

Sub VariableOlymics()
'Run this macro as many times as you'd like, with an activesheet ready for data
'in cells B2 to D6
Dim beginTIME As Double, trials As Long, i As Long, p As Long

    trials = 1000000000
    p = 0

    beginTIME = Now
    For i = 1 To trials
        Call boomBYTE
    Next i
    Call Finished(p, Now - beginTIME, CDbl(trials))
    p = p + 1

    beginTIME = Now
    For i = 1 To trials
        Call boomINTEGER
    Next i
    Call Finished(p, Now - beginTIME, CDbl(trials))
    p = p + 1

    beginTIME = Now
    For i = 1 To trials
        Call boomLONG
    Next i
    Call Finished(p, Now - beginTIME, CDbl(trials))
    p = p + 1

    beginTIME = Now
    For i = 1 To trials
        Call boomDOUBLE
    Next i
    Call Finished(p, Now - beginTIME, CDbl(trials))
    p = p + 1

    beginTIME = Now
    For i = 1 To trials
        Call boomUNDECLARED
    Next i
    Call Finished(p, Now - beginTIME, CDbl(trials))
    p = p + 1

End Sub

Private Sub boomBYTE()
Dim a As Byte, b As Byte, c As Byte

    a = 1
    b = 1 + a
    c = 1 + b
    c = c + 1

End Sub

Private Sub boomINTEGER()
Dim a As Integer, b As Integer, c As Integer

    a = 1
    b = 1 + a
    c = 1 + b
    c = c + 1

End Sub

Private Sub boomLONG()
Dim a As Long, b As Long, c As Long

    a = 1
    b = 1 + a
    c = 1 + b
    c = c + 1

End Sub

Private Sub boomDOUBLE()
Dim a As Double, b As Double, c As Double

    a = 1
    b = 1 + a
    c = 1 + b
    c = c + 1

End Sub

Private Sub boomUNDECLARED()

    a = 1
    b = 1 + a
    c = 1 + b
    c = c + 1

End Sub

Private Sub Finished(i As Long, timeUSED As Double, trials As Double)

    With Range("B2").Offset(i, 0)
            .Value = .Value + trials
            .Offset(0, 1).Value = .Offset(0, 1).Value + timeUSED
            .Offset(0, 2).FormulaR1C1 = "=ROUND(RC[-1]*3600*24,0)"
    End With

End Sub

@Vityata 2018-09-04 21:41:13

The undeclared variable is a variant. Dim a as Variant, b as Variant, c As Variant should give the same result as not "dimming" at all.

@RubberDuck 2018-09-04 23:33:25

Neat! Happy to see someone did some benchmarking!

@PGCodeRider 2018-09-07 07:00:39

@Vityata, I was aware that "undeclared" SHOULD mean variant, but keep in mind the backstory to this whole discussion was microsoft automatically converting integer to Long despite being defined! So I wasn't sure what to expect with whole numbers without being defined. Seeing the result, it seems most likely to truly be Variants, but that's why I gave the name "Undeclared" vs. 'Variant." Plus that's probably a lot more useful to lower level coders. However, I'll go ahead and add that in now. Thanks for feedback.

@Vityata 2018-09-07 08:38:52

@PGCodeRider - it would be probably interesting to create a dll library with c++ and to add it to VBA and to benchmark it there as well. The results would be probably interesting. I have done something similar here -…

@RubberDuck 2014-10-16 16:35:37

Integer variables are stored as 16-bit (2-byte) numbers


Long (long integer) variables are stored as signed 32-bit (4-byte) numbers


So, the benefit is in reduced memory space. An Integer takes up half the memory that a long does. Now, we are talking about 2 bytes, so it's not going to make a real difference unless you're storing a TON of integers.

BUT on a 32 bit system, a 16 bit integer gets silently converted to a long without the benefit of the larger range of numbers to work with. Overflows still happen and it takes just as much memory. Performance may even be hurt because the datatype has to be converted (at a very low level).

Not the reference I was looking for but....

My understanding is that the underlying VB engine converts integers to long even if its declared as an integer. Therefore a slight speed decrease can be noted. I have believed this for some time and perhaps thats also why the above statement was made, I didnt ask for reasoning.

ozgrid forums

This is the reference I was looking for.

Short answer, in 32-bit systems 2 byte integers are converted to 4 byte Longs. There really is no other way so that respective bits correctly line up for any form of processing. Consider the following

MsgBox Hex(-1) = Hex(65535) ' = True

Obviously -1 does not equal 65535 yet the computer is returning the correct answer, namely "FFFF" = "FFFF"

However had we coerced the -1 to a long first we would have got the right answer (the 65535 being greater than 32k is automatically a long)

MsgBox Hex(-1&) = Hex(65535) ' = False


Generally there is no point in VBA to declare "As Integer" in modern systems, except perhaps for some legacy API's that expect to receive an Integer.

pcreview forum

And at long last I found the msdn documentation I was really truly looking for.

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're declared as type Integer. So there's no longer a performance advantage to using Integer variables; in fact, Long variables may be slightly faster because VBA does not have to convert them.

So, in summary, there's almost no good reason to use an Integer type these days. Unless you need to Interop with an old API call that expects a 16 bit int.

One thing worth pointing out is that some old API functions may be expecting parameters that are 16-bit (2-byte) Integers and if you are on a 32 bit and trying to pass an Integer (that is already a 4-byte long) by reference it will not work due to difference in length of bytes.

Thanks to Vba4All for pointing that out.

@user2140173 2014-11-04 09:04:32

One thing worth pointing out is that some old API functions may be expecting parameters that are 16-bit (2-byte) Integers and if you are on a 32 bit and trying to pass an Integer (that is already a 4-byte long) by reference it will not work due to difference in length of bytes.

@user1220978 2015-04-26 07:16:22

@It'sbeenapleasure I'don't understand. If you pass a Long by ref to a function that expects an Integer by ref, it will use the first two bytes, and since numbers are stored in little-endian, it will work (provided that only the two lower bytes are meaningful, but it's likely if this Long is simply an Integer stored in 32 bits).

@ThunderFrame 2017-04-09 20:01:47

The other time that you must use Integer is when declaring a Type, where the type's layout and size is important, either because you're passing the type to an API, or you're serializing/deserializing a file, or you're copying bytes with LSet/Rset.

@GSerg 2017-09-03 13:06:49

That MSDN article is wrong and/or misleading. It might refer to e.g. Integers being internally processed as Longs inside a processor, but for all observable purposes an Integer still takes two bytes. See answers and comments under for extended discussion.

@RubberDuck 2017-09-03 13:10:43

@GSerg I explicitly state that in my answer. Do you have a suggestion for how to make that more clear?

@GSerg 2017-09-03 13:18:37

You don't seem to state anywhere in your answer that… might be wrong or misleading, rather, you are presenting it as the msdn documentation I was really truly looking for. However my comment was mostly directed towards future readers rather than yourself, because you have participated in that other question, even though your answer there in my opinion should not have been accepted (this one I believe should have been).

@RubberDuck 2018-09-04 23:30:42

I just noticed someone had edited this answer with incorrect links to the VB.Net documentation back in July. Please don't do that. That documentation is flat out wrong as far as VBA is concerned.

@Alter 2014-10-16 17:32:59

This is a space vs necessity problem.

In some situations it's a necessity to use a long. If you're looping through rows in a large excel file, the variable that holds the row number should be a long.

However, sometimes you will know that an integer can handle your problem and using a long would be a waste of space (memory). Individual variables really don't make much of a difference, but when you start dealing with arrays it can make a big difference.

  • In VBA7, Integers are 2 bytes and longs are 4 bytes

  • If you have an array of 1 million numbers between 1 and 10, using an Integer array would take up about 2MB of RAM, compared to roughly 4MB of RAM for a long array.

@ChrisB 2017-08-31 16:51:04

For an array of numbers between 1 and 10, you could use a BYTE array instead, but I understand the point you are making.

@Vityata 2018-09-05 10:24:43

This was probably true some 15 years ago, but now the answer is really outdated.

@Alter 2018-09-05 14:05:02

How is this outdated? Problems in pandas where you have millions of rows (big data/machine learning), or in networking when transferring large amounts of data (ex. physics experiments)

@Vityata 2018-09-07 08:42:34

@Alter - I guess you missed the point of the original question. Take a look at the accepted answer.

@Alter 2018-09-07 12:13:09

Looks like I understood it. Also the accepted answer points out it's talking about 32 bit systems when it says there's no difference.

@Alex K. 2014-10-16 16:30:37

VBA has a lot of historical baggage.

An Integer is 16 bits wide and was a good default numeric type back when 16 bit architecture/word sizes were prevalent.

A Long is 32 bits wide and (IMO) should be used wherever possible.

@John Babson 2014-10-16 16:15:35

Perhaps a CS grad can explain it better, but yes an integer requires less memory to store (may not be relevant in a very simple VBA script).

It also guarantees/ validates that the variable will always be an integer (non-decimal). I'm fairly sure if you store 6.7 as an integer, it will be converted to the number 7 in VBA.

It may not be the best practice to do this, but there may be cases where this is needed. I think the main purpose is data validation/ integrity. In particular, yes for loops where something needs to occurs X amount of times -- a decimal wouldn't make sense.

Primary difference is memory though -- if I recall a long is 64 bit and integer is 32 bit.

@RubberDuck 2014-10-16 16:31:53

That's the .Net storage space, not VBA.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Convert a string to an integer in JavaScript?

64 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3788453 View
  • 3206 Score
  • 64 Answer
  • Tags:   java random integer

31 Answered Questions

[SOLVED] How do I detect unsigned integer multiply overflow?

21 Answered Questions

[SOLVED] Generate random integers between 0 and 9

  • 2010-10-22 12:48:29
  • aneuryzm
  • 1619167 View
  • 1128 Score
  • 21 Answer
  • Tags:   python random integer

13 Answered Questions

[SOLVED] Converting an integer to a string in PHP

7 Answered Questions

[SOLVED] What is the argument for printf that formats a long?

  • 2008-09-01 22:45:25
  • Thomas Owens
  • 668097 View
  • 428 Score
  • 7 Answer
  • Tags:   c printf long-integer

1 Answered Questions

Changing how a variable is declared based on the size of the worksheet?

  • 2016-05-13 09:36:38
  • user1996971
  • 26 View
  • 1 Score
  • 1 Answer
  • Tags:   excel vba

8 Answered Questions

1 Answered Questions

Reliably detect integer overflow/underflow

2 Answered Questions

[SOLVED] What is this VBA primitive data type?

  • 2012-02-16 21:36:38
  • JKK
  • 930 View
  • 0 Score
  • 2 Answer
  • Tags:   vba

Sponsored Content