【问题标题】:Macro to insert rows and all possibilities based on a count基于计数插入行和所有可能性的宏
【发布时间】:2015-03-10 09:27:39
【问题描述】:

这是我在这里的第一篇文章,但我过去经常访问以寻求帮助。

我正在寻找一个宏来根据集合中的项目数减去 1 在每个条目之间插入特定数量的行,然后插入每个组合。

我会尽力举例。

假设我有大量的收藏品,每个收藏品都是独一无二的。我想与该集合中的每个项目建立一对一的关系。因此,如果我有集合Red 与项目123 以及集合Blue 与项目ABCE 和@987654 .

我的工作簿中有 3 列。

Collection, Primary Item, Relationship Item
Row 1 = Red, 1.
Row 2 = Red, 2.
Row 3 = Red, 3.
Row 4 = Blue, A.
Row 5 = Blue, B.
Row 6 = Blue, C.
Row 7 = Blue, D.
Row 8 = Blue, E.

我的目标是在 Red 集合的每一行之间插入 2 行,在 Blue 集合的每一行之间插入 4 行。然后复制新空白列中的每个集合和目标项以及关系项中的每个相关项。

这会变成这样:

Collection, Target Item, Relationship Item
Row 1 = Red, 1, 2.
Row 2 = Red, 1, 3.
Row 3 = Red, 2, 1.
Row 4 = Red, 2, 3.
Row 5 = Red, 3, 1.
Row 6 = Red 3, 2.
Row 7 = Blue, A, B.
Row 8 = Blue, A, C.
Row 9 = Blue, A, D.
Row 10 = Blue, A, E.
Row 11 = Blue, B, A.
Row 12 = Blue, B, C.
Row 13 = Blue, B, D.
Row 14 = Blue, B, E.
Row 15 = Blue, C, A.
Row 16 = Blue, C, B.

等等。

这将需要多次使用,其中包含不同数量的集合和项目。

我希望我的描述已经足够详细,如果您对如何简化我的解释有任何问题或建议,请告诉我。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我建议您输出到单独的工作表而不是插入现有的工作表。 这将使您的参数文件保持独立以进行校对并简化循环。

    接下来,您需要一个嵌套循环机制来创建所有可能的组合。 试试这个链接的循环。 Creating a list of all possible unique combinations from an array (using VBA)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多