+ Reply to Thread
Results 1 to 2 of 2

Seeking help updating VBA script for generating all combinations using columns

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2022
    Location
    US
    MS-Off Ver
    Office 2016
    Posts
    1

    Seeking help updating VBA script for generating all combinations using columns

    So i have a VBA script that I'm using to generate all possible combinations using 4 columns of data. The output is needing to be formatted a certain way as well. Currently this is what I have:

    Option Explicit
    
    Sub Sample()
        Dim i As Long, j As Long, k As Long, l As Long
        Dim CountComb As Long, lastrow As Long
    
        Range("G2").Value = Now
    
        Application.ScreenUpdating = False
    
        CountComb = 0: lastrow = 6
    
        For i = 1 To 15: For j = 1 To 23
        For k = 1 To 9: For l = 1 To 11
            Range("G" & lastrow).Value = "key=" & Range("A" & i).Value & "&details=" & _
                                         Range("B" & j).Value & "/" & _
                                         Range("C" & k).Value & "/" & _
                                         Range("D" & l).Value
            lastrow = lastrow + 1
            CountComb = CountComb + 1
        Next: Next
        Next: Next
    
        Range("G1").Value = CountComb
        Range("G3").Value = Now
    
        Application.ScreenUpdating = True
    End Sub
    It does indeed work to a certain extent, however the issues are that it only seems to return combinations of all 4 columns, when I'd like for it to return combinations using 1-3 columns as well. Essentially like this:

    key=1&details=2
    key=1&details=2/3
    key=1&details=2/3/4
    key=1&details=2/3/4/5
    Also, I somehow was able to get 1/2/3 to work, however it keeps adding a / on the end if the combination uses less than all 4 columns which is messing up what I'm using it for. How would I go about generating the combinations without a trailing / ? Any help with this would be greatly appreciated, I've been stuck on it for hours. Thanks!

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,376

    Re: Seeking help updating VBA script for generating all combinations using columns

    This ??

    xstr = "key=" & Range("A" & i).Value & "&details=" & _
                                         Range("B" & j).Value & "/" & _
                                         Range("C" & k).Value & "/" & _
                                         Range("D" & l).Value
            If Right(xstr, 1) = "/" Then Range("G" & lastrow).Value = Left(xstr, Len(xstr) - 1) _
            Else Range("G" & lastrow).Value = xstr
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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. Generating Combinations
    By kiprotich in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2020, 11:23 AM
  2. Seeking help creating Automatic updating workbook
    By Excelintern2017 in forum Excel General
    Replies: 2
    Last Post: 07-26-2017, 06:36 AM
  3. [SOLVED] Generating full list of scenarios (or combinations) of data in columns
    By cparker347 in forum Excel General
    Replies: 6
    Last Post: 07-28-2015, 10:28 AM
  4. generating all possible combinations
    By tuckertuesday in forum Excel General
    Replies: 3
    Last Post: 12-10-2014, 06:24 PM
  5. generating combinations
    By fern4500 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-11-2012, 08:47 AM
  6. Help generating combinations
    By JamRock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2006, 03:40 PM
  7. Generating excel combinations
    By mark4006 in forum Excel General
    Replies: 2
    Last Post: 03-06-2005, 01:06 PM

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