Consulting

Results 1 to 7 of 7

Thread: Excel Maximum Rows

  1. #1
    VBAX Regular
    Joined
    Apr 2020
    Posts
    10
    Location

    Excel Maximum Rows

    I am using following codes to generate combination of 5 numbers from 45 numbers. Because Excel only can handle 1048576 rows, it gives me an error message (Run-time error '1004': Method 'Range' of object'_Global' failed). Is there anything else I can do to resolve the issue?

    Sub GenerateCombo()
        'Ref PCG
        Dim rRng As Range, p
        Dim vElements, lRow As Long, vresult As Variant
        Set rRng = Range("A1", Range("A1").End(xlDown))
        p = 5
        vElements = Application.Index(Application.Transpose(rRng), 1, 0)
        ReDim vresult(1 To p)
        Call CombinationsNP(vElements, CInt(p), vresult, lRow, 1, 1)
    End Sub
     
    Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, lRow As Long, iElement As Integer, iIndex As Integer)
         Dim i As Integer
         For i = iElement To UBound(vElements)
          vresult(iIndex) = vElements(i)
        
         If iIndex = p Then
                lRow = lRow + 1
                Range("B" & lRow) = Join(vresult, ", ")
                Range("C" & lRow).Resize(, p) = vresult 'Multi column Result
        Else
    
     Call CombinationsNP(vElements, p, vresult, lRow, i + 1, iIndex + 1)
    
     End If
      Next i
    End Sub
    Last edited by Kalpesh; 03-17-2021 at 04:27 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    to generate combination of 5 numbers from 45 numbers
    Why would you ?

  3. #3
    VBAX Regular
    Joined
    Apr 2020
    Posts
    10
    Location
    Quote Originally Posted by snb View Post
    Why would you ?
    To generate combination.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Do you like circles ?

  5. #5
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    Adding link for crosspost: LINK

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    To me the macros seemed more complicated that they had to be

    This is another way


    Option Explicit
    
    
    Sub GenerateCombo()
        Dim r As Range
        Dim v As Variant
        Dim n1 As Long, n2 As Long, n3 As Long, n4 As Long, n5 As Long
        Dim rowCount As Long, colCount As Long
        Dim N As Long
        
        With ActiveSheet
            Set r = .Range("A1")
            Set r = Range(r, r.End(xlDown))
            
            v = Application.WorksheetFunction.Transpose(r)
            N = UBound(v)
            
            rowCount = 1
            colCount = 3
            
            For n1 = 1 To N - 4
                For n2 = n1 + 1 To N - 3
                    For n3 = n2 + 1 To N - 2
                        For n4 = n3 + 1 To N - 1
                            For n5 = n4 + 1 To N
                                .Cells(rowCount, colCount).Value = v(n1) & "," & v(n2) & "," & v(n3) & "," & v(n4) & "," & v(n5)
                                rowCount = rowCount + 1
                                If rowCount = 100001 Then
                                    rowCount = 1
                                    colCount = colCount + 1
                                    Application.StatusBar = v(n1) & "," & v(n2) & "," & v(n3) & "," & v(n4) & "," & v(n5)
                                    DoEvents
                                End If
                            Next n5
                        Next n4
                    Next n3
                Next n2
            Next n1
        End With
        
        Application.StatusBar = False
        
        MsgBox "Done"
        
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •