Excel排列,不会重复多列中的值

时间:2014-03-27 13:26:41

标签: excel excel-vba excel-2007 combinations permutation vba

Excel文档中包含的内容:

A     B    
Abc   12:34
Def   56:78
Ghi   90:12
Jkl   34:56
...

我想用这些价值实现的目标:

C    D      E    F
Abc  12:34  Def  56:78
Abc  12:34  Ghi  90:12
Abc  12:34  Jkl  34:56
Def  56:78  Ghi  90:12
Def  56:78  Jkl  34:56
Ghi  90:12  Jkl  34:56
...

说明:

A列和B列可以包含文本和数字的任意组合(如果这一点很重要),此示例仅显示最常见的结构。它应该只为行创建组合"在向下的路上",i。即"美国广播公司...进行..."这已经足够了,不应该......#Def; Abc ......"。

周围有很多例子,但我很难找到一个适用于多列并且不会重复组合的VBA版本。

这是一个简单的例子。但是,它只适用于一列,它也会重复值:

http://www.mrexcel.com/forum/excel-questions/412952-create-list-all-pair-combinations.html#post2046893

提前谢谢。

1 个答案:

答案 0 :(得分:2)

鉴于我们在对话中讨论的内容,这里是VBA的解决方案:

Sub CreatePermutation()

Dim FirstCell As Integer
Dim SecondCell As Integer
Dim NumRows As Integer
Dim OutputRow As Long

    ' Get the total number of rows in column A
    NumRows = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row()

    ' You want to start outputting in row 1
    OutputRow = 1

    For FirstCell = 1 To NumRows - 1
        For SecondCell = FirstCell + 1 To NumRows

            ' Put in the data from the first cell into columnc C & D
            Cells(OutputRow, 3).Value = Cells(FirstCell, 1).Value
            Cells(OutputRow, 4).Value = Cells(FirstCell, 2).Value

            ' Put in the data from the second cell into column E & F
            Cells(OutputRow, 5).Value = Cells(SecondCell, 1).Value
            Cells(OutputRow, 6).Value = Cells(SecondCell, 2).Value

            ' Move to the next row to output
            OutputRow = OutputRow + 1

        Next SecondCell
    Next FirstCell
End Sub

希望这能完成你想要做的事。

相关问题