Long is the VBA data type you should reach for whenever you are counting rows, storing IDs, or handling any whole number that might grow beyond about 32,000. Declare the variable with Dim myNumber As Long, assign a value, and Excel handles the rest — up to roughly 2.1 billion. The most common reason people get a Run-time error 6: Overflow in Excel VBA is that they used Integer where a Long belonged, and switching the declaration fixes it in seconds. This guide walks through declaring the type, choosing it correctly, converting existing code safely, and diagnosing the traps that trip people up.
What You'll Need
- Microsoft Excel on Windows or Mac with the Developer tab visible (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac). Menu labels can differ slightly by version, device, and region.
- A workbook saved as
.xlsm(macro-enabled) if you want the code to persist. A plain.xlsxwill silently drop your macros on save. - Access to the Visual Basic Editor, opened with Alt+F11 on Windows or the Developer > Visual Basic button on either platform.
- A basic grasp of variables: what declaring, assigning, and reading one means.
Step-by-Step
- Open the Visual Basic Editor with Alt+F11 on Windows (fn+Option+F11 on some Macs), or click Developer > Visual Basic. If you have never enabled macros, Excel may block them; check File > Options > Trust Center > Trust Center Settings > Macro Settings and enable macros for the current session only while you develop.
- Insert a module. In the Project Explorer, right-click your workbook name and choose Insert > Module. Code placed in a standard module is easier to test than code in a worksheet's own code pane.
- Start your procedure with
Sub TestLong()on one line and press Enter. The editor addsEnd Subautomatically. If your editor is set to require variable declaration, a line readingOption Explicitmay already sit at the top of the module — leave it there, because it forces you to declare everything and catches typos early. - Declare the variable by typing
Dim rowCount As Long. The type name isLong, spelled as one word with a capital L out of habit. This single line tells VBA to reserve space for a whole number ranging from about −2,147,483,648 to 2,147,483,647. - Assign a value on the next line:
rowCount = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row. This is the classic last-row finder, and it is exactly the kind of expression that overflows if stored in anInteger, becauseRows.Countin a modern worksheet is 1,048,576. - Show the result with
MsgBox rowCount, then run the macro with F5. If the editor asks which macro to run, selectTestLong. Confirm the number returned matches the last filled row in column A. - Declare related variables together rather than one per line if you prefer compactness:
Dim i As Long, lastRow As Long, total As Long. Note that each name still needs its own type —Dim i, j As Longleavesias a Variant. - Use it in a loop where counting matters:
For i = 2 To lastRowthen readCells(i, 2).Value. Long handles loop counters over a million rows without complaint. - Convert explicit type suffixes if you have old code. The ampersand suffix declares or coerces a Long:
Dim total&is identical toDim total As Long, andCLng(value)converts an expression to Long at runtime. UseCLngwhen pulling a numeric string from a text box that may exceed 32,000. - Consider LongLong if you truly need more. VBA7 (the version shipped with Office 2010 and later) supports
Dim bigNum As LongLong, a 64-bit type for values beyond roughly 2.1 billion — for example, file sizes in bytes. It is not available in older Office builds, so test before shipping a workbook to colleagues on mixed versions. - Save and test with your real data. Run the macro on the sheet with the most rows you expect to encounter. Doing this once, before you rely on the code, exposes overflow before users see it.
Common Mistakes
Using Integer out of habit. VBA's Integer caps at 32,767. Modern worksheets have over a million rows, so any row variable should be Long. On 32-bit and 64-bit VBA alike, Long is not slower in any way that matters for spreadsheet work — the old advice to prefer Integer dates from 16-bit systems and no longer applies.
Typing Long Integer. There is no data type called "Long Integer" in VBA. The type is Long; the words "long integer" describe it in documentation and in other languages. Dim x As Long Integer will produce a compile error.
Forgetting Option Explicit. Without it, a misspelled variable name silently creates a new Variant, and your calculations return zero or empty results with no warning.
Assuming Long fixes decimals. Assign 3.75 to a Long and VBA rounds to 4. If you need fractions, use Double or Currency, not Long.
Overflowing in intermediate arithmetic. Two Longs multiplied can exceed the Long range before assignment. Coerce first with CLng on a Double, or switch to LongLong, or restructure the calculation.
Tips
- Declare row and column variables as Long by default; the cost is negligible and the safety gain is large.
- Use
CLngrather thanCIntwhen converting numbers you did not generate yourself — text boxes, CSV imports, user input — becauseCIntwill overflow on perfectly ordinary values. - Keep
Option Explicitat the top of every module. The editor can add it automatically via Tools > Options > Require Variable Declaration. - When debugging an overflow, hover over the variable in break mode (F8 to step) and check its value before the failing line.
- Prefix names clearly, such as
lngRow, if your team uses Hungarian notation — but the type declaration matters far more than the prefix.
Troubleshooting
"Run-time error 6: Overflow." The value exceeded the variable's range. Find the declaration for the variable on the highlighted line; if it says Integer, change it to Long. If it is already Long, the number itself exceeds about 2.1 billion — use LongLong or split the calculation.
"Compile error: User-defined type not defined." You likely wrote Dim x As Long Integer or used LongLong in an older Office version. Change to Long.
The macro returns 0 unexpectedly. A variable is probably undeclared and silently Variant. Add Option Explicit and recompile with Debug > Compile VBAProject.
"Expected: end of statement." Check for a missing space or stray character, and confirm you did not type two type names after As.
Numbers get rounded. That is correct Long behaviour — it holds whole numbers only. Switch the type if you need decimals.
If none of that resolves it, step through the procedure line by line with F8 and watch each variable in the Locals window. The line where the value jumps unexpectedly is almost always the source.
Frequently Asked Questions
What is the range of a Long in VBA?
Approximately −2,147,483,648 to 2,147,483,647 — a signed 32-bit integer. Anything outside that range triggers error 6.
Is Long faster or slower than Integer?
For practical spreadsheet code, the difference is irrelevant. Long is the safer default and is the recommended type for all counters and row references.
What is the difference between Long and LongLong?
LongLong is a signed 64-bit integer with a vastly larger range, available in VBA7 (Office 2010 and later). Use it only when Long overflows, since older Office installations cannot run code that declares it.
How do I convert a value to Long?
Use CLng(expression). It rounds to the nearest whole number rather than truncating, unlike Int.
Can Long store decimal numbers?
No. Assigning a decimal rounds it to a whole number. Use Double for fractions and Currency for money with fixed decimal places.