Excel VBA 宏来生成可能的组合列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17681098/
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 VBA macro to generate a list of possible combinations
提问by user2588002
I am trying to write an excel macro to generate a list of possible combinations.
我正在尝试编写一个 excel 宏来生成可能组合的列表。
I have a fixed series of values in different columns
我在不同的列中有一系列固定的值
A, B, C, D - each in it's own column.
A、B、C、D - 每个都在它自己的列中。
I have a list of new values to replace with A, B, C or D and would like to generate all of the combinations.
我有一个要替换为 A、B、C 或 D 的新值列表,并希望生成所有组合。
List of new values i.e. 1, 2, 3, 4
新值列表,即 1、2、3、4
I would get a total of 16 different combinations
我总共会得到 16 种不同的组合
For example,
例如,
1BCD
2BCD
3BCD
4BCD
A1CD
A2CD
A3CD
1BCD
2BCD
3BCD
4BCD
A1CD
A2CD
A3CD
...
ABC1
ABC2
ABC3
ABC4
...
ABC1
ABC2
ABC3
ABC4
I'm not sure if this is clear but I would like to generate combinations by iterating through each column to generate the possible combinations with the new values inserted.
我不确定这是否清楚,但我想通过遍历每一列来生成组合,以生成插入新值的可能组合。
回答by Jon Crowell
You can use the following to cross join two different ranges. It will handle ranges of any size and write the crossjoined combinations to a target sheet that you specify.
您可以使用以下内容交叉连接两个不同的范围。它将处理任何大小的范围并将交叉连接的组合写入您指定的目标表。
In the example below, I have defined two named ranges: newValues
and fixedValues
. Both of these ranges are on Sheet1
. I then loop through the ranges and write all combinations to Sheet2
.
在下面的示例中,我定义了两个命名范围:newValues
和fixedValues
. 这两个范围都在Sheet1
。然后我遍历范围并将所有组合写入Sheet2
.
Sub CrossJoinMyRanges()
Dim ws As Worksheet
Dim newValues As Range
Dim cell As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set newValues = ws.Range("newValues")
' loop through the new values
For Each cell In newValues
Call ReplaceMe(cell.Value, ws)
Next cell
End Sub
Sub ReplaceMe(replacement As String, ws As Worksheet)
Dim fixedValues As Range
Dim cell As Range
Set fixedValues = ws.Range("fixedValues")
' outer loop through fixedValues
For Each cell In fixedValues
Call PrintReplacedValues(cell.Row, replacement)
Next cell
End Sub
Sub PrintReplacedValues(rowNumber As Long, replacement As String)
Dim wb As Workbook
Dim src As Worksheet
Dim tgt As Worksheet
Dim fixedValues As Range
Dim cell As Range
Dim printMe As String
Dim x As Long, y As Long
Set wb = ThisWorkbook
Set src = wb.Sheets("Sheet1")
Set tgt = wb.Sheets("Sheet2")
Set fixedValues = src.Range("fixedValues")
y = 1
x = tgt.Range("A" & tgt.Rows.Count).End(xlUp).Row + 1
' inner loop through fixed values
For Each cell In fixedValues
' replace the fixed value with the replacement
' if the loops intersect
If cell.Row = rowNumber Then
printMe = replacement
Else
' otherwise keep the fixed value
printMe = cell
End If
' write to the target sheet
tgt.Cells(x, y).Value = printMe
y = y + 1
Next cell
End Sub
There are a few similar questions with alternative solutions that you can also look into if my approach isn't what you were after:
如果我的方法不是你所追求的,你也可以研究一些类似的问题和替代解决方案:
Excel vba to create every possible combination of a Range