VBA - Runtime Error 6 - Overflow

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
961
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
The following code gives me a Runtime Error 6 Overflow
Cell Range A2 through A26 contain the values 1 through 25

Thank you in advance.

Option Explicit

Dim Nb As Integer, NbRange As Range
Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer
Dim I As Integer, J As Integer, sTxt(58906) As String


' Show Pents
Set NbRange = Range("A2:A26")
Nb = Application.WorksheetFunction.CountA(NbRange)
Range("A1").Select
I = 1
For A = 1 To Nb - 4
For B = A + 1 To Nb - 3
For C = B + 1 To Nb - 2
For D = C + 1 To Nb - 1
For E = D + 1 To Nb
sTxt(I) = ActiveCell.Offset(A, 0).Value & "--" & ActiveCell.Offset(B, 0).Value _
& "--" & ActiveCell.Offset(C, 0).Value & "--" & ActiveCell.Offset(D, 0).Value _
& "--" & ActiveCell.Offset(E, 0).Value
I = I + 1
Next E
Next D
Next C
Next B
Next A
For J = 1 To I - 1
ActiveCell.Offset(J, 3).Value = sTxt(J)
Next J
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Which line gives the error?
 
Upvote 0
Declare I and J as Long, not Integer.
 
Upvote 0
Solution
You'll probably find it quicker to use a 2D array and dump it to the worksheet in one go at the end:

VBA Code:
   Dim Nb As Integer, NbRange As Range
   Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer
   Dim I As Long, sTxt(1 To 58906, 1 To 1) As String
   
   
   ' Show Pents
   Set NbRange = Range("A2:A26")
   Nb = Application.WorksheetFunction.CountA(NbRange)
   Range("A1").Select
   I = 1
   For A = 1 To Nb - 4
      For B = A + 1 To Nb - 3
         For C = B + 1 To Nb - 2
            For D = C + 1 To Nb - 1
               For E = D + 1 To Nb
                  sTxt(I, 1) = ActiveCell.Offset(A, 0).Value & "--" & ActiveCell.Offset(B, 0).Value _
                  & "--" & ActiveCell.Offset(C, 0).Value & "--" & ActiveCell.Offset(D, 0).Value _
                  & "--" & ActiveCell.Offset(E, 0).Value
                  I = I + 1
               Next E
            Next D
         Next C
      Next B
   Next A
   ActiveCell.Offset(, 3).Resize(I - 1).Value = sTxt
 
Upvote 0

Forum statistics

Threads
1,215,314
Messages
6,124,202
Members
449,147
Latest member
sweetkt327

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top