vba Excel 排列不重复来自多列中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22689075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel permutations without repetition from values in multiple columns
提问by take2
What I have in Excel document:
我在 Excel 文档中有什么:
A B
Abc 12:34
Def 56:78
Ghi 90:12
Jkl 34:56
...
What I want to achieve with those values:
我想用这些价值观实现的目标:
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
...
Explanation:
解释:
Columns A and B can contain any combination of text and numbers (if that is important at all), this example only displays the most common structure. It should create combinations only for rows "on the way down", i. e. "Abc...Def..." is enough, there shouldn't be "Def...Abc...".
A 列和 B 列可以包含文本和数字的任意组合(如果这很重要的话),此示例仅显示最常见的结构。它应该只为“在路上”的行创建组合,即“Abc...Def...”就足够了,不应该有“Def...Abc...”。
There are many examples around, but I am struggling to find a version of such VBA that works with multiple columns and doesn't repeat combinations.
周围有很多例子,但我正在努力寻找这样的 VBA 版本,它可以处理多列并且不重复组合。
Here is one example that is simple. But, it's for only one column and it also repeats values:
这是一个简单的例子。但是,它仅用于一列,并且还会重复值:
Thank you in advance.
先感谢您。
回答by John Bustos
Given what we discussed in the conversation, here's a solution in VBA:
鉴于我们在对话中讨论的内容,以下是 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
Hope this accomplishes what you're looking to do.
希望这能完成你想要做的事情。