Overcoming Run-Time Error 6 in Excel Macros: Handling Large Field Sizes
Overcoming Run-Time Error 6 in Excel Macros: Handling Large Field Sizes

Are you struggling with Run-Time Error 6 (Overflow) in your Excel macros? You’re not alone. Many users encounter this issue when working with large datasets. This article will delve into the root causes of this error, provide practical solutions, and offer advanced tips to manage large field sizes efficiently.
Written By: Ada Codewell – AI Specialist & Software Engineer at Gray Technical
Why Does Run-Time Error 6 Happen?
Run-Time Error 6 occurs when a calculation or operation exceeds the data type limit in VBA. In Excel, this often happens with large datasets or when variables are not properly defined to handle large numbers.
Step-by-Step Solution
Example 1: Counting Records with Large Field Sizes

Let’s start by addressing the CountRecordsFIELDSIZE macro that counts race numbers in column N. If you’re dealing with field sizes of approximately 39,119 rows, you might encounter overflow errors.
Sub CountRecordsFIELDSIZE()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim count As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "N").End(xlUp).Row
count = 0
For i = 1 To lastRow
If IsNumeric(ws.Cells(i, "N").Value) Then
count = count + 1
End If
Next i
MsgBox "Total records: " & count
End Sub
Example 2: Ranking Macros with Large Field Sizes

If you’re dealing with a ranking macro that handles large field sizes, consider the following code:
Sub RankMacro()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim rankArr() As Variant
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
ReDim rankArr(1 To lastRow)
For i = 1 To lastRow
rankArr(i) = ws.Cells(i, "F").Value
Next i
QuickSort rankArr, LBound(rankArr), UBound(rankArr)
' Place sorted values back in column G
For i = 1 To lastRow
ws.Cells(i, "G").Value = rankArr(i)
Next i
End Sub
Sub QuickSort(arr As Variant, ByVal firstIndex As Long, ByVal lastIndex As Long)
Dim pivotIndex As Long
If firstIndex < lastIndex Then
pivotIndex = Partition(arr, firstIndex, lastIndex)
QuickSort arr, firstIndex, pivotIndex - 1
QuickSort arr, pivotIndex + 1, lastIndex
End If
End Sub
Function Partition(arr As Variant, ByVal firstIndex As Long, ByVal lastIndex As Long) As Long
Dim pivotValue As Double
Dim temp As Double
Dim i As Long
Dim j As Long
pivotValue = arr(lastIndex)
i = firstIndex - 1
For j = firstIndex To lastIndex - 1
If arr(j) <= pivotValue Then
i = i + 1
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next j
temp = arr(i + 1)
arr(i + 1) = arr(lastIndex)
arr(lastIndex) = temp
Partition = i + 1
End Function

Example 3: Automating Date Entry in Tables
If you need to automatically populate the current date in a table, use this VBA code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Not Intersect(Target, Me.Range("A:A")) Is Nothing Then
Application.EnableEvents = False
For Each cell In Intersect(Target, Me.Range("A:A"))
If cell.Value "" And IsDate(cell.Value) = False Then
cell.Value = Date
End If
Next cell
Application.EnableEvents = True
End If
End Sub

Advanced Variation: Using Long Data Type for Large Arrays
When dealing with very large datasets, ensure that your arrays can handle the size:
Sub HandleLargeArrays()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim rankArr() As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
ReDim rankArr(1 To lastRow)
For i = 1 To lastRow
rankArr(i) = CLng(ws.Cells(i, "F").Value)
Next i
' Sort array and output results here
End Sub

Common Mistakes or Misconceptions
One common mistake is not properly declaring variable types. Always explicitly declare your variables, especially when dealing with large datasets.
Another mistake is not using optimized sorting algorithms for large arrays.

Conclusion
Overcoming Run-Time Error 6 in Excel macros involves understanding the data type limits and optimizing your code. By using proper variable declarations and efficient algorithms, you can handle large datasets without errors.

For more advanced Excel tools, consider checking out [CelTools](https://www.graytechnical.com/celtools/) which offers 70+ extra features for auditing, formulas, and automation.






















