+ Reply to Thread
Results 1 to 5 of 5

Combine cells to have all possibilities of the combined cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    5

    Combine cells to have all possibilities of the combined cells

    I have two columns
    Column A which has letters A, B, C until Z
    Column B which has letters 1, 2, 3 until 9
    I would like a macro to return the combination of every cell in Column A with every cell in column B in column C
    i.e. it should return in Column C, A 1, A 2, A 3 until A 9 and then B 1, B 2 until B 9 until Z 9.
    Thanks for the help.

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Combine cells to have all possibilities of the combined cells

    Hi,

    try this:
    Sub Combine()
    
    'declare variables
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    
    'fill alphabet letters to column A
    For i = 1 To 26
        Cells(i, 1) = Mid(Cells(1, i).Address, 2, 1)
    Next i
    
    'fill numbers to column B
    For i = 1 To 9
        Cells(i, 2) = i
    Next i
    
    'fill combinations to column C
    For i = 1 To 26
        For j = 1 To 9
            Cells(k + 1, "C") = Cells(i, "A") & Cells(j, "B")
            k = k + 1
        Next j
    Next i
    
    End Sub

  3. #3
    Registered User
    Join Date
    10-29-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Combine cells to have all possibilities of the combined cells

    Thanks for the above macro.
    However if the above columns varies, sometimes column A can have less/more then 26 and column B can have less/more then 9, where in the code do I change the number of fields in the column.
    Also Column A inst limited to one letter of the alphabet
    and Column B isn't limited to Numbers it could sometimes be text.

    Thanking for the help in advance

  4. #4
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Combine cells to have all possibilities of the combined cells

    This code will look at how many rows there are in columns A and B and act accordingly. If you want to change he 'for' cycles, feel free to do so. The way I use it, it goes from "x to y", so if you want to change the start or the end, change those parts.
    For i = 1 To 26
    'you can change 1 or 26 to whatever you want (well, almost :P)
    Sub Combine()
    
    'declare variables
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    
    'fill combinations to column C
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row 'go from 1 to number of rows in A
        For j = 1 To Cells(Rows.Count, "B").End(xlUp).Row 'go from 1 to number of rows in B
            Cells(k + 1, "C") = Cells(i, "A") & Cells(j, "B")
            k = k + 1
        Next j
    Next i
    
    End Sub
    I'm going to sleep now so don't stay up waiting for me to reply.

  5. #5
    Registered User
    Join Date
    10-29-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Combine cells to have all possibilities of the combined cells

    Thank you very much you have saved hours of work for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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