VBA trying to copy each unique combination of whole columns to a new sheet

PiPaPils

New Member
Joined
Feb 7, 2018
Messages
9
Hi all!

I usually lurk in these forums for all my VBA questions. There are several real nuggets of advice and solutions. I am always able to combine code and learn of the solutions offered. However, I can't get the following problem fixed... Basically I got a dynamic amount of columns in one sheet. I want to compare all possible combinations of columns in a separate sheet. So if I got Column A t/m F on the main sheet, I want a sheet for column A & B, one sheet for A & C, one sheet for A & D, etc... until the last sheet that consists of E & F.


Main sheet:Sheet 1:Sheet 2:Sheet 3:
ColumnAColumnBColumnCColumnDColumnEColumnFColumnAColumnBColumnAColumnCColumnAColumnD
161317121619161316171612
85910348589810
131115161318131113151316
131662021913161361320
111851581811181151115
141617151319141614171415
91091092091099910
26315192623215
61820151415618620615
10148211201014108102
15115819111511515158
1651217201116516121617
18101373818101813187
1517966181517159156
141531414914151431414
108151410710810151014
15718199115715181519
1971427101971914192

<tbody>
</tbody>

The issue is that the amount of columns is dynamic. So sometimes its 10, sometimes 15 and it can run up to 20. I am currently struggling to get it done with a double column loop, but I think I am thinking in the wrong direction.

Is there anyone who can give me a push in the right direction :) ?

Thanks in advance,

Koen
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
What is the first combination after you process the Columns A/F combination... ColumnB/ColumnC or ColumnB/ColumnA (the reverse of the first Column A processing)?
 
Upvote 0
Try:
Code:
Sub CopyCols()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    lColumn = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    Dim x As Long
    For x = 2 To lColumn
        Sheets.Add after:=Sheets(Sheets.Count)
        Sheets("Sheet1").Range("A1:A" & LastRow).Copy Cells(1, 1)
        Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, x), Sheets("Sheet1").Cells(LastRow, x)).Copy Cells(1, 2)
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub CopyCols()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    lColumn = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    Dim x As Long
    For x = 2 To lColumn
        Sheets.Add after:=Sheets(Sheets.Count)
        Sheets("Sheet1").Range("A1:A" & LastRow).Copy Cells(1, 1)
        Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, x), Sheets("Sheet1").Cells(LastRow, x)).Copy Cells(1, 2)
    Next x
    Application.ScreenUpdating = True
End Sub

Hi Mumps!

Thats certainly a step in the right direction! It runs "only" from A & B to A & F however. I could added the blue part to make it run for all B combinations as well. Ideally there is a way to loop this as well ofcourse :)

Code:
Sub CopyCols()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    lColumn = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    Dim x As Long
    For x = 2 To lColumn
        Sheets.Add after:=Sheets(Sheets.Count)
        Sheets("Sheet1").Range("A1:A" & LastRow).Copy Cells(1, 1)
        Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, x), Sheets("Sheet1").Cells(LastRow, x)).Copy Cells(1, 2)
    Next x
    
    
[COLOR=#0000ff][I]    For x = 3 To lColumn[/I][/COLOR]
[COLOR=#0000ff][I]        Sheets.Add after:=Sheets(Sheets.Count)[/I][/COLOR]
[COLOR=#0000ff][I]        Sheets("Sheet1").Range("B1:B" & LastRow).Copy Cells(1, 1)[/I][/COLOR]
[COLOR=#0000ff][I]        Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, x), Sheets("Sheet1").Cells(LastRow, x)).Copy Cells(1, 2)[/I][/COLOR]
[COLOR=#0000ff][I]    Next x[/I][/COLOR]
    
    Application.ScreenUpdating = True
    
    
    
    
    
    
    
End Sub
@Rick, the combination is not important since I am planning to use a worksheet loop afterwards. I want to run several statistical tests on every possible combination, and then combine it again in a summary sheet. I do not necessarily need both A & B and B & A, but if that's the simplest solution, they are welcome :)
 
Upvote 0
@Rick, the combination is not important since I am planning to use a worksheet loop afterwards. I want to run several statistical tests on every possible combination, and then combine it again in a summary sheet. I do not necessarily need both A & B and B & A, but if that's the simplest solution, they are welcome :)
Okay, give this macro a try. The code assumes your data is on a sheet named "Main" and it will insert a new sheet for each column combination that you want and name that inserted sheet with the column designations its data came from.
Code:
Sub ColumnsTwoByTwo()
  Dim R As Long, C As Long, Coff As Long, LastRow As Long, LastCol As Long, LastSht As String
  LastRow = Sheets("Main").Cells(Rows.Count, "A").End(xlUp).Row
  LastCol = Sheets("Main").Cells(1, Columns.Count).End(xlToLeft).Column
  LastSht = "Main"
  For C = 1 To LastCol - 1
    For Coff = C + 1 To LastCol
      With Sheets.Add(After:=Sheets(LastSht))
        .Name = Split(Columns(C).Address(0, 0), ":")(0) & "-" & Split(Columns(Coff).Address(0, 0), ":")(0)
        LastSht = .Name
        .Range("A1:B" & LastRow) = Application.Index(Sheets("Main").Cells, Evaluate("ROW(1:" & LastRow & ")"), Split(C & " " & Coff))
      End With
    Next
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,259
Messages
6,123,919
Members
449,135
Latest member
NickWBA

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