Excel VBA。 需要一个macros来随机排列(3p3)3次而不重复任何排列

我有9个单元格(我认为有3个单元格),每个单元格包含字母A,B和C.这三个单元之间的顺序是平衡的,因此没有两个字母在同一个位置,例如:

ABCCABBAC

我有3个其他单元格包含字母d,e和f。 我想用下面的规则把d,e和f分配给A,B和C:

  1. d,e和f在每个组中都被分别一次。
  2. d,e和f分别与A,B和C组合一次

例如 :

Ad Be Cf Ce Af Bd Bf Ae Cd

基本上我想写一个VBAmacros,它给了我随机分配给ABC遵循这些规则。 我希望这个macros把这个包含def的单元格放在包含AB C的单元格下面。

对不起,我甚至没有试图写代码来做到这一点,也很抱歉,如果这是一个混乱/格式不好的问题。 我是VBA和stackexchange的新手,解释这个问题有点麻烦。

干杯

如果在一行的开头有四个空格,则以固定大小的字体输出该行。 这用于代码,但也可以让你创build你想要的图像。 我最好的猜测是你有这样的事情:

| A | B | C | D | E | F | G | H | I | J | --+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ 1 | A | B | C | | | | d | e | f | | 2 | | | | | | | | | | | 3 | | | | | | | | | | | 

从这个开始位置,下面的代码创build:

  | A | B | C | D | E | F | G | H | I | J | --+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ 1 | A | B | C | | | | d | e | f | | 2 | | | | | | | | | | | 3 | Ad | Ae | Af | Bd | Be | Bf | Cd | Ce | Cf | | 

我的输出值与您的输出值不同,但不在您的规则列表中。

你说你有三个版本的第一组值,每个可能的序列中有一个。 你不解释第二和第三个版本的目的,所以我没有包括它们。

如果这不是你想要的,你必须提供一个更全面的解释你的要求。

 Option Explicit Sub Permutate() Dim ColDestCrnt As Long Dim ColSrc1First As Long Dim ColSrc1Crnt As Long Dim ColSrc1Last As Long Dim ColSrc2First As Long Dim ColSrc2Crnt As Long Dim ColSrc2Last As Long Dim RowSrc1 As Long Dim RowSrc2 As Long Dim RowDest As Long ' Specify the position of the first set of source values RowSrc1 = 1 ColSrc1First = 1 ' Column A ColSrc1Last = 3 ' Column C ' Specify the position of the second set of source values RowSrc2 = 1 ColSrc2First = 6 ' Column F ColSrc2Last = 8 ' Column H ' Specify the start the destination RowDest = 3 ColDestCrnt = 1 With Worksheets("Sheet1") ' Loop through first set of values and within that loop through ' the second set. For ColSrc1Crnt = ColSrc1First To ColSrc1Last For ColSrc2Crnt = ColSrc2First To ColSrc2Last ' Combine one value from the first set with one value in the second set. .Cells(RowDest, ColDestCrnt).Value = _ .Cells(RowSrc1, ColSrc1Crnt).Value & _ .Cells(RowSrc2, ColSrc2Crnt).Value ColDestCrnt = ColDestCrnt + 1 Next Next End With End Sub