【问题标题】:EXCEL-VBA: Concatenate n rows with each line of another columnEXCEL-VBA:将 n 行与另一列的每一行连接起来
【发布时间】:2022-01-21 12:58:17
【问题描述】:

我一直坚持我认为是一个简单的动作来询问 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

我试图查看现有的问题,但可能我不知道使用哪些关键字,我找不到好的代码。我找到的最接近的是this,但我无法“翻译”提到的伪代码:我迷失了提到的“迭代”

你能帮帮我吗?

非常提前发送

【问题讨论】:

    标签: excel vba concatenation


    【解决方案1】:

    这也可以使用 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)中提供的 Power Query 来完成

    使用 Power Query

    • 选择数据表中的某个单元格
    • Data => Get&Transform => from Table/Range
    • 当 PQ 编辑器打开时:Home => Advanced Editor
    • 记下第 2 行中的表 Name
    • 粘贴下面的 M 代码代替您看到的内容
    • 将第 2 行中的表名称更改回最初生成的名称。
    • 阅读 cmets 并探索 Applied Steps 以了解算法

    M 代码
    如果您的列名不是自动生成的,您可能需要将 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"
    

    【讨论】:

    • 有启发性和帮助性 +:)
    【解决方案2】:

    如果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
    

    【讨论】:

    • ... 只是一些提示:a) 使用Option Explicit 强制变量类型声明; b) 完全限定您的范围引用(例如,通过工作表代码(名称)Sheet1.Range("A1") 或表格工作表名称字符串,例如ThisWorkbook.Worksheets("MySheetName").Range("A1")),因为默认情况下引用的当前活动工作表不必是焦点;考虑使用With - End With 结构; d)avoid using Select in VBA; e) 单元格中的循环与数据字段数组中的循环相比更耗时
    猜你喜欢
    • 2012-02-17
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2020-03-01
    相关资源
    最近更新 更多