[英]EXCEL-VBA: Concatenate n rows with each line of another column
I am stuck with what I though was a simple action to ask excel.我坚持认为是一个简单的动作来询问 excel。 I need to concatenate each and every line of a column with each of the column of a second column.
我需要将一列的每一行与第二列的每一列连接起来。 This is what I would like to do
这就是我想做的
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![]() |
I tried to look into the existing questions, but maybe I don't know which keywords to use, I can't find a good code.The closest I found is this but I can't "translate" the pseudocode mentioned: I'm lost with the "iterration" mentionned我试图研究现有的问题,但也许我不知道要使用哪些关键字,我找不到好的代码。我找到的最接近的是这个但我无法“翻译”提到的伪代码:I'我迷失了提到的“迭代”
Could you please help me?请你帮助我好吗?
Txs very much in advance非常提前Txs
If column C have only a title, below code does the work:如果列 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
This can also be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)这也可以使用 Power Query 来完成,在 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)中可用
To use Power Query使用 Power Query
Data => Get&Transform => from Table/Range
Home => Advanced Editor
Home => Advanced Editor
Applied Steps
to understand the algorithmApplied Steps
以了解算法M Code M代码
If your column names are not auto-generated, you may need to edit Column1
and Column2
to whatever the actual column names are (eg: Column1=>ColumnA
)如果您的列名不是自动生成的,您可能需要将
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.