+ Reply to Thread
Results 1 to 4 of 4

Excel VBA - Combination - Complicated Problem

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2018
    Location
    ATLANTA, US
    MS-Off Ver
    Microsoft 365 for Enterprise
    Posts
    11

    Post Excel VBA - Combination - Complicated Problem

    Hi,

    I have a model, which can increase up to 20 characters. Right now, it is at 6. I have listed the replacement below. a can only be replaced with A. b can only be replaced with B and so forth and so on. I want to change depending on the variation chosen, from 2, 3, 4, or 5.


    Model: a b c d e f
    Replacement: A B C D E

    2 Variation (10 combinations)
    A B c d e
    A b C d e
    A b c D e
    A b c d E
    a B C d e
    a B c D e
    a B c d E
    a b C D e
    a b C d E
    a b c D E

    3 Variation (6 Combinations)
    A B C d e
    A b C D e
    A b c D E
    a B C D e
    a B c D E
    a b C D E

    4 Variation (3 Combinations)
    A B C D e
    A b C D E
    a B C D E

  2. #2
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: Excel VBA - Combination - Complicated Problem

    Sub Combinations()
         Dim i1 As Integer, i2 As Integer, N, k, Aux, Out, iComb, ptr, t
    
         On Error Resume Next
         i1 = InputBox("number of letters", 1)     'how many letters in your string
         i2 = InputBox("number of capitals", 1)     'how many with capital
         On Error GoTo 0
         i2 = Application.Min(i1, i2)     'both numeric ? otherwise 0
         If i2 = 0 Then MsgBox "no combinations": Exit Sub
    
         t = Timer
    
         N = i1
         k = i2
         For i = 1 To i1
              s0 = s0 & Chr(96 + i)     'make your string
         Next
         Aux = Evaluate("=column(offset(a1,,,," & i2 & "))")     'start with 1,2,3,... as many as capitals
         iComb = Application.Min(Rows.Count, WorksheetFunction.Combin(N, k))     'number of combinations possible (less then the number of rows in a sheet !!!)
         ReDim Out(1 To iComb, 1 To 1)     'prepare array
    
         Do
              ptr = ptr + 1     'pointer
              s = s0     'initial string
              For i = 1 To UBound(Aux)
                   s = Left(s, Aux(i) - 1) & UCase(Mid(s, Aux(i), 1)) & Mid(s, Aux(i) + 1, 99)     'replace several characters with their capital
              Next
              Out(ptr, 1) = s     'add in array
              Aux(k) = Aux(k) + 1     'next combination
              If Aux(k) > N Then                  'laatste voorbij target !
                   For i = k - 1 To 1 Step -1            'voorgaande kolommen aflopen
                        If Aux(i) < N - (k - i) Then     'tot aan die kolom die nog 1 mag opgehoogd worden
                             Aux(i) = Aux(i) + 1         'die kolom 1 ophogen
                             For j = i + 1 To k         'alle volgende kolommen
                                  Aux(j) = Aux(j - 1) + 1     'gelijk aan de vorige kolom +1
                             Next
                             Exit For                          'wip uit de loop
                        End If
                   Next
              End If
         Loop While ptr < iComb
    
         With Range("A1")
              .EntireColumn.ClearContents
              .Resize(UBound(Out)).Value = Out
              .EntireColumn.AutoFit
              .Offset(, 1).Value = UBound(Out)
         End With
    
         MsgBox Format(iComb, "#,###") & " combinations in " & Format(Timer - t, "0.0\s")
    End Sub
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    02-06-2018
    Location
    ATLANTA, US
    MS-Off Ver
    Microsoft 365 for Enterprise
    Posts
    11

    Re: Excel VBA - Combination - Complicated Problem

    Thanks again!
    Last edited by carguel1; 08-08-2022 at 10:44 PM.

  4. #4
    Registered User
    Join Date
    02-06-2018
    Location
    ATLANTA, US
    MS-Off Ver
    Microsoft 365 for Enterprise
    Posts
    11

    Re: Excel VBA - Combination - Complicated Problem

    Wow, thank you so much! You're the best!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 03-20-2016, 11:06 AM
  2. Excel Combination Generator (overflow problem)
    By nkodb37 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2015, 02:31 PM
  3. Using Excel Solver for a complicated capacity problem
    By RandallTex in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-25-2013, 10:15 AM
  4. Excel 2007 Complicated Auto-sort/Formatting Problem
    By Rossington in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-04-2011, 12:43 PM
  5. Complicated Problem!
    By moonyboy99 in forum Excel General
    Replies: 2
    Last Post: 07-17-2008, 08:14 AM
  6. Replies: 3
    Last Post: 10-27-2006, 05:53 AM
  7. Quite a complicated problem
    By DaBeef in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-17-2006, 11:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1