';Nchoosek';或Excel查询
数据采用以下格式,每行一个数据集:';Nchoosek';或Excel查询,excel,octave,vba,Excel,Octave,Vba,数据采用以下格式,每行一个数据集: Column A Column B Abc 123 Def 456 Ghi 789 Jkl 000 我试图列出三个数据集的所有可能组的组合。例如: Column A. B. C. D. E. F. Abc 123 Def
Column A Column B
Abc 123
Def 456
Ghi 789
Jkl 000
我试图列出三个数据集的所有可能组的组合。例如:
Column A. B. C. D. E. F.
Abc 123 Def 456 Ghi 789
Abc 123 Def 456 Jkl 000
Def 456 Ghi 789 Jkl 000
Abc. 123 Ghi 789 JKl 000
实际上,实际列数超过150,行数超过50,因此不适用 您可以从您的链接中获得更一般的方法。我的代码应该能够处理可变的列数
Sub CreatePermutation()
Dim lFirstCellRow As Long
Dim lSecondCellRow As Long
Dim lThirdCellRow As Long
Dim lNumRows As Long
Dim iNumCols As Integer
Dim lOutputRow As Long
With ActiveSheet
'start your source data in A1 without empty cells
lNumRows = .Cells(1, 1).End(xlDown).Row()
If lNumRows = .Rows.Count Then lNumRows = 1
iNumCols = .Cells(1, 1).End(xlToRight).Column()
If iNumCols = .Columns.Count Then iNumCols = 1
'target data will begin after one empty row below the source data
lOutputRow = lNumRows + 2
For lFirstCellRow = 1 To lNumRows - 2
For lSecondCellRow = lFirstCellRow + 1 To lNumRows - 1
For lThirdCellRow = lSecondCellRow + 1 To lNumRows
.Range(.Cells(lOutputRow, 1), .Cells(lOutputRow, iNumCols)).Value = .Range(.Cells(lFirstCellRow, 1), .Cells(lFirstCellRow, iNumCols)).Value
.Range(.Cells(lOutputRow, 1 + iNumCols), .Cells(lOutputRow, 2 * iNumCols)).Value = .Range(.Cells(lSecondCellRow, 1), .Cells(lSecondCellRow, iNumCols)).Value
.Range(.Cells(lOutputRow, 1 + 2 * iNumCols), .Cells(lOutputRow, 3 * iNumCols)).Value = .Range(.Cells(lThirdCellRow, 1), .Cells(lThirdCellRow, iNumCols)).Value
lOutputRow = lOutputRow + 1
Next lThirdCellRow
Next lSecondCellRow
Next lFirstCellRow
End With
End Sub
到目前为止,您编码了什么?请显示您代码的当前状态。为什么不
Abc 123 Ghi 789 Jkl 000
?如Axel所指出,为清晰起见,已添加第四种可能性。谢谢