excel - 如何根据标准连接单元格范围

标签 excel vba concatenation

我在 excel 中有 2 列,如下所示:

CutNo    Data       
1        A          
1        B
1        C

2        A          
2        B

3        A  
如果 Cut No 相同,我想连接列数据的数据并将其放在另一个名为 Concatenate 的列中并计算出现次数并将其放在另一列中,如下所示
CutNo    Data       Concatenate     Occurrences
1        A          A & B & C           1 
1        B
1        C

2        A          A & B               1
2        B

3        A          A                   1
我使用以下代码
    Sub Unique()
    Dim Rng, Cel As Range
    Dim lr As Long
    Dim x As Integer
    Dim str As String
    lr = Sheets("Report").Cells(Rows.count, 1).End(xlUp).Row
    Set Rng = Sheets("Report").Range("A2:A" & lr)
    For x = 1 To Rng.count
    For Each Cel In Rng.Cells
    If Cel.Value = x Then
    str = str & Rng.Cells(x, 1).Offset(0, 7) & ","
    End If
    Next Cel
    Rng.Cells(x, 1).Offset(0, 10).Value = str
    Next x
    End Sub
我没有得到我需要的正确结果,
感谢您的支持
感谢和问候
莫赫布·拉比

最佳答案

如果您有 Excel O365 和 FILTER函数,你不需要VBA:
( 注意: 我假设 Occurrences 可以通过计算 CutNo 的行数来计算。如果你的意思是别的,请澄清)

C2: =IF(AND(A2<>A1,A2<>""),TEXTJOIN(" & ",TRUE,FILTER($B:$B,$A:$A=A2)),"")
D2: =IF(AND(A2<>A1,A2<>""),COUNTIF($A:$A,A2),"")
并填写。
enter image description here
您也可以使用 Power Query 来执行此操作在 Excel 2010+ 中可用
  • 选择要包含的整个范围
  • *无法自动选择,因为有空白行

  • 在 Excel 2016+ 中:Data --> Get & Transform --> From Table/Range
  • 我不确定早期版本,您可以在其中下载免费的 MS 插件以实现此功能。

  • 当 PQ 编辑器打开时,选择 Home --> Advanced Editor并粘贴 M Code下面进入打开的窗口。
  • 将第 2 行中的 Table name 更改为打开 PQ 时生成的 Table 的名称。


  • 有关解释,请检查“应用步骤”窗口中的项目。如果您将光标悬停在任何 i图标,您将看到相关的评论;如果你双击一个齿轮,它会打开一个对话窗口,这样你就可以检查做了什么。
  • 关闭并加载到:我选择原始数据旁边的列,但还有其他方法可以做到这一点。

  • M码
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"CutNo", Int64.Type}, {"Data", type text}}),
    
        //make the grouping easier, else we'd have a group with the blank rows
        #"Removed Blank Rows" = Table.SelectRows(#"Changed Type", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
        
        //Group by CutNo -- hence no need to sort
        #"Grouped Rows" = Table.Group(#"Removed Blank Rows", {"CutNo"}, {{"Grouped", each _, type table [CutNo=nullable number, Data=nullable text]}}),
    
        //add a blank row at the bottom of each grouped table (each CutNo group)
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "addBlankRow", each Table.InsertRows([Grouped],
                Table.RowCount([Grouped]),
                {[CutNo=null, Data=null]})),
    
        //remove unneded columns
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"CutNo", "Grouped"}),
        #"Added Custom1" = Table.AddColumn(#"Removed Columns", "Custom", each Table.Column([addBlankRow],"Data")),
    
        //Concatenate the "Data"
        #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"Custom", each Text.Combine(List.Transform(_, Text.From), " & "), type text}),
    
        //Count the rows (subtract one since last row will be blank
        #"Added Custom2" = Table.AddColumn(#"Extracted Values", "Custom.1", each Table.RowCount([addBlankRow])-1),
    
        //Expand the Table column to put a blank row between each group of CutNo
        #"Expanded addBlankRow" = Table.ExpandTableColumn(#"Added Custom2", "addBlankRow", {"CutNo"}, {"addBlankRow.CutNo"}),
    
        //Add Index column so we can null out where there should be empty cells in the Concatenate Column
        #"Added Index" = Table.AddIndexColumn(#"Expanded addBlankRow", "Index", 0, 1, Int64.Type),
        #"Added Custom3" = Table.AddColumn(#"Added Index", "Concatenate", each 
            if [Index] = 0 
                then [Custom]
                else if [addBlankRow.CutNo] = null 
                then null 
                else if [addBlankRow.CutNo] = #"Expanded addBlankRow"[addBlankRow.CutNo]{[Index]-1} 
                then null 
                else [Custom]),
    
        //Blank cells in the Occurrence column if blank in the CutNo column
        #"Added Custom4" = Table.AddColumn(#"Added Custom3", "Occurrences", each 
            if [Concatenate] = null then null 
            else [Custom.1]),
    
        //Remove unneeded columns        
        #"Removed Columns1" = Table.RemoveColumns(#"Added Custom4",{"addBlankRow.CutNo", "Custom", "Custom.1", "Index"}),
    
        //Remove bottom row which will be blank
        #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Columns1",1)
    in
        #"Removed Bottom Rows"
    
    enter image description here

    关于excel - 如何根据标准连接单元格范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63545630/

    相关文章:

    ruby-on-rails - 在 .erb 文件中使用 link_to,串联语法

    function - Application.MacroOptions 和错误 1004

    C#读取Excel文件时出错,这是一个奇怪的错误

    excel - 在特定工作表/Excel 上调用宏

    excel - vba的FindNext不起作用

    excel - VBA 中的工作表函数

    vba - 从 MS Excel 自定义对话框中的控件获取值

    excel - .NullString 属性在输出中将 "-"显示为 0 的问题

    python - 绕过pandas concat错误 "Reindexing only valid with uniquely valued Index objects"

    string - 从命令行 cmd 启动 Windows 10 UWP 应用程序