简体   繁体   English

EXCEL-VBA:将 n 行与另一列的每一行连接起来

[英]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 A栏 Column B B栏 Result结果
AA AA XX XX AA XX AA XX
BB BB YY YY AA YY啊啊啊啊
CC抄送 ZZ Z Z AA ZZ啊啊啊啊
..n ..n BB XX BBXX
BB YY BBYY
BB ZZ BB ZZ
CC XX抄送二十
CC YY抄送
CC ZZ CC ZZ
AA..n AA..n
BB..n BB..n
CC..n抄送..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

  • Select some cell in your Data Table Select 数据表中的某个单元格
  • Data => Get&Transform => from Table/Range
  • When the PQ Editor opens: Home => Advanced Editor当 PQ 编辑器打开时: Home => Advanced Editor
  • Make note of the Table Name in Line 2记下第 2 行中的表
  • Paste the M Code below in place of what you see粘贴下面的 M 代码代替您看到的内容
  • Change the Table name in line 2 back to what was generated originally.将第 2 行中的表名称更改回最初生成的名称。
  • Read the comments and explore the Applied Steps to understand the algorithm阅读评论并探索Applied 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 )如果您的列名不是自动生成的,您可能需要将Column1Column2编辑为实际的列名(例如: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM