英文:
VBA for combining new columns into a table involving extra work on displaying all possible combinations
问题
我有一个表格,例如:
国家 | 性别 |
---|---|
A | 男性 |
B | 女性 |
然后我有3个爱好:睡觉、跑步和玩耍。
然后我有2个团队:红队和蓝队。
我想在VBA中实现的目标是复制该表格,并在其后添加爱好和团队列,直到列出所有组合。换句话说,这是一个列出组合的问题,但我们需要将表格作为整体视为组合的一个维度:
国家 | 性别 | 爱好 | 团队 |
---|---|---|---|
A | 男性 | 睡觉 | 红队 |
B | 女性 | 睡觉 | 红队 |
A | 男性 | 睡觉 | 蓝队 |
B | 女性 | 睡觉 | 蓝队 |
A | 男性 | 跑步 | 红队 |
B | 女性 | 跑步 | 红队 |
A | 男性 | 跑步 | 蓝队 |
B | 女性 | 跑步 | 蓝队 |
A | 男性 | 玩耍 | 红队 |
B | 女性 | 玩耍 | 红队 |
A | 男性 | 玩耍 | 蓝队 |
B | 女性 | 玩耍 | 蓝队 |
我想首先列出所有爱好和团队的组合。爱好有3个值,团队有2个。
然后,我考虑遍历“国家性别”表格中的每一行,并将“爱好和团队”的6种可能组合添加到其中。
但我想我技术上还不足以编写这个。
谢谢!
英文:
I have a table, for example:
Country | Gender |
---|---|
A | Male |
B | Female |
Then I have 3 hobbies: sleep, run and play.
Then I have 2 teams: red and blue.
What I want to achieve in vba is to duplicate the table and add hobby and team column after it until all combinations are listed out. In other words, this is a listing combination question, but we need to treat a table as a whole to be a dimension in the combination:
Country | Gender | Hobby | Team |
---|---|---|---|
A | Male | Sleep | Red |
B | Female | Sleep | Red |
A | Male | Sleep | Blue |
B | Female | Sleep | Blue |
A | Male | Run | Red |
B | Female | Run | Red |
A | Male | Run | Blue |
B | Female | Run | Blue |
A | Male | Play | Red |
B | Female | Play | Red |
A | Male | Play | Blue |
B | Female | Play | Blue |
I was thinking first we can list out all combinations of hobby and team. Hobby has 3 values, team has 2.
Then I am thinking we can loop through the each of row within "Country Gender" table and adding the 6 possible combinations of "hobby and team" to it.
But I guess I am just technically not good enough to write this.
thx
答案1
得分: 2
以下是您提供的代码的翻译部分:
// 将下一行更改为反映实际数据源
Source = Excel.CurrentWorkbook(){[Name="Table21"]}[Content],
#"更改类型" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Gender", type text}}),
// 添加包含爱好和团队列表的列
#"添加爱好" = Table.AddColumn(#"更改类型", "Hobby",
each {"睡觉","跑步","玩游戏"}),
#"添加团队" = Table.AddColumn(#"添加爱好", "Team",
each {"红队","蓝队"}),
// 展开列表以获取新行
#"展开爱好" = Table.ExpandListColumn(#"添加团队", "Hobby"),
#"展开团队" = Table.ExpandListColumn(#"展开爱好", "Team"),
// 排序以按照您发布的顺序排列(如果您不想要此步骤,请省略)
#"排序行" = Table.Sort(#"展开团队",{
{"Hobby", Order.Descending},
{"Team", Order.Descending},
{"Gender", Order.Descending}})
in
#"排序行"
希望这对您有所帮助!如果您需要任何进一步的帮助,请随时告诉我。
英文:
This can also be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)
To use Power Query
- Select some cell in your Data Table
Data => Get&Transform => from Table/Range
- When the PQ Editor opens:
Home => Advanced Editor
- Make note of the Table Name in Line 2
- Paste the M Code below in place of what you see
- Change the Table name in line 2 back to what was generated originally.
- Read the comments and explore the
Applied Steps
to understand the algorithm
let
//Change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table21"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Gender", type text}}),
//Add columns with a list of Hobbys and Teams
#"Add Hobby" = Table.AddColumn(#"Changed Type", "Hobby",
each {"Sleep","Run","Play"}),
#"Add Team" = Table.AddColumn(#"Add Hobby", "Team",
each {"Red","Blue"}),
//Expand the lists to new rows
#"Expanded Hobby" = Table.ExpandListColumn(#"Add Team", "Hobby"),
#"Expanded Team" = Table.ExpandListColumn(#"Expanded Hobby", "Team"),
//Sort to get to your posted order (or omit this step if you want
#"Sorted Rows" = Table.Sort(#"Expanded Team",{
{"Hobby", Order.Descending},
{"Team", Order.Descending},
{"Gender", Order.Descending}})
in
#"Sorted Rows"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论