EXCEL-VBA:将n行与另一列的每一行连接起来
EXCEL-VBA: Concatenate n rows with each line of another column
我坚持认为这是一个简单的问题 excel。
我需要将一列的每一行与第二列的每一列连接起来。
这就是我想做的
Column A
Column B
Result
AA
XX
AA XX
BB
YY
AA YY
CC
ZZ
AA ZZ
..n
BB XX
BB YY
BB ZZ
CC XX
CC YY
CC ZZ
AA ..n
BB ..n
CC ..n
我试图调查现有的问题,但也许我不知道要使用哪些关键字,我找不到好的 code.The 我发现最接近的是 this 但我可以不要“翻译”提到的伪代码:我迷失了提到的“迭代”
你能帮帮我吗?
非常提前发送
如果 C 列只有一个标题,下面的代码可以完成工作:
Sub test()
Range("A1").Select ' Select 1st row of column A
Selection.End(xlDown).Select ' Move to last row in column A
last_col_A = ActiveCell.Row ' Get last row number in column A
Range("B1").Select ' Select 1st row of column B
Selection.End(xlDown).Select ' Move to last row in column B
last_col_B = ActiveCell.Row ' Get last row number in column A
row_C = 2 ' Start from second row of column C
For rowA = 2 To last_col_A
For rowB = 2 To last_col_B
Range("C" & row_C).Value = Range("A" & rowA).Value & " " & Range("B" & rowB).Value
row_C = row_C + 1 ' Increment row number
Next
Next
End Sub
这也可以使用 Power Query 完成,可用于 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)
使用 Power Query
- Select 数据中的某个单元格 Table
Data => Get&Transform => from Table/Range
- 当 PQ 编辑器打开时:
Home => Advanced Editor
- 记下第 2 行中的 Table 名称
- 粘贴下面的 M 代码代替您看到的内容
- 将第 2 行中的 Table 名称更改回最初生成的名称。
- 阅读评论并探索
Applied Steps
以了解算法
M码
如果您的列名不是 auto-generated,您可能需要将 Column1
和 Column2
编辑为实际的列名(例如:Column1=>ColumnA
)
let
//change Table name in next line to actual table name
Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
//set data types
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Column1", type text}, {"Column2", type text}}),
//for each entry in Column 1, create a List of all the entries in Column 2
//then remove column 2 and expand the custom column
#"Added Custom" = Table.AddColumn(#"Changed Type", "List of all in Column2",
each #"Changed Type"[Column2]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column2"}),
#"Expanded List of all in Column2" = Table.ExpandListColumn(
#"Removed Columns", "List of all in Column2"),
//remove the rows with nulls
// (happens if one column longer than the other
#"Filtered Rows" = Table.SelectRows(#"Expanded List of all in Column2",
each ([Column1] <> null) and ([List of all in Column2] <> null)),
//merge the two columns with space delimiter to get Results
#"Merged Columns" = Table.CombineColumns(#"Filtered Rows",
{"Column1", "List of all in Column2"},
Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Result")
in
#"Merged Columns"
我坚持认为这是一个简单的问题 excel。 我需要将一列的每一行与第二列的每一列连接起来。 这就是我想做的
Column A | Column B | Result |
---|---|---|
AA | XX | AA XX |
BB | YY | AA YY |
CC | ZZ | AA ZZ |
..n | BB XX | |
BB YY | ||
BB ZZ | ||
CC XX | ||
CC YY | ||
CC ZZ | ||
AA ..n | ||
BB ..n | ||
CC ..n |
我试图调查现有的问题,但也许我不知道要使用哪些关键字,我找不到好的 code.The 我发现最接近的是 this 但我可以不要“翻译”提到的伪代码:我迷失了提到的“迭代”
你能帮帮我吗?
非常提前发送
如果 C 列只有一个标题,下面的代码可以完成工作:
Sub test()
Range("A1").Select ' Select 1st row of column A
Selection.End(xlDown).Select ' Move to last row in column A
last_col_A = ActiveCell.Row ' Get last row number in column A
Range("B1").Select ' Select 1st row of column B
Selection.End(xlDown).Select ' Move to last row in column B
last_col_B = ActiveCell.Row ' Get last row number in column A
row_C = 2 ' Start from second row of column C
For rowA = 2 To last_col_A
For rowB = 2 To last_col_B
Range("C" & row_C).Value = Range("A" & rowA).Value & " " & Range("B" & rowB).Value
row_C = row_C + 1 ' Increment row number
Next
Next
End Sub
这也可以使用 Power Query 完成,可用于 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)
使用 Power Query
- Select 数据中的某个单元格 Table
Data => Get&Transform => from Table/Range
- 当 PQ 编辑器打开时:
Home => Advanced Editor
- 记下第 2 行中的 Table 名称
- 粘贴下面的 M 代码代替您看到的内容
- 将第 2 行中的 Table 名称更改回最初生成的名称。
- 阅读评论并探索
Applied Steps
以了解算法
M码
如果您的列名不是 auto-generated,您可能需要将 Column1
和 Column2
编辑为实际的列名(例如:Column1=>ColumnA
)
let
//change Table name in next line to actual table name
Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
//set data types
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Column1", type text}, {"Column2", type text}}),
//for each entry in Column 1, create a List of all the entries in Column 2
//then remove column 2 and expand the custom column
#"Added Custom" = Table.AddColumn(#"Changed Type", "List of all in Column2",
each #"Changed Type"[Column2]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column2"}),
#"Expanded List of all in Column2" = Table.ExpandListColumn(
#"Removed Columns", "List of all in Column2"),
//remove the rows with nulls
// (happens if one column longer than the other
#"Filtered Rows" = Table.SelectRows(#"Expanded List of all in Column2",
each ([Column1] <> null) and ([List of all in Column2] <> null)),
//merge the two columns with space delimiter to get Results
#"Merged Columns" = Table.CombineColumns(#"Filtered Rows",
{"Column1", "List of all in Column2"},
Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Result")
in
#"Merged Columns"