简体   繁体   English

在Excel 2010中,如何在包含多个值单元格的单元格范围内删除重复项并连接值?

[英]In Excel 2010, how could I remove duplicates and concatenate values within a cell range that includes multiple values cells?

I made a document in Excel 2010 however, the functionality I'm hoping to get from it doesn't seem to be possible (at least not with the default Excel functions) and I don't know enough about VB programming to make my own UDF. 我在Excel 2010中创建了一个文档但是,我希望从中获取的功能似乎不可能(至少没有使用默认的Excel函数)而且我对VB编程的了解不够UDF。 (I'm actually using one I found online which does part of what I want, but doesn't meet all of my needs.) (我实际上使用的是我在网上找到的一部分,它可以满足我的所有需求,但不能满足我的所有需求。)

Let me break it down: 让我分解一下:

  1. I have multiple sheets with groups of fields where users can add numbers (some will be blank, some will contain a single number, some will contain multiple comma-separated numbers) 我有多个字段组,其中用户可以添加数字(一些将为空,一些将包含一个数字,一些将包含多个以逗号分隔的数字)

  2. I have an "Overview" sheet where I want to Concatenate those numbers (and remove any duplicates) within a few different sections (only looking at specific field groups). 我有一个“概述”表,我想在几个不同的部分(仅查看特定的字段组)中连接这些数字(并删除任何重复)。

I found a ConcatIf UDF that works fairly well for this, however it can't handle non-consecutive cells to concatenate (For example, I want to concatenate and remove duplicates from cells D30, G30, J30 and M30 together) (Here's the UDF:) 我找到了一个相当好的ConcatIf UDF,但它不能处理连续的非连续单元格(例如,我想连接并删除单元格D30,G30,J30和M30中的重复项)(这里是UDF :)

Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _ 
Optional Delimiter As String, Optional NoDuplicates As Boolean) As String 
Dim i As Long, j As Long 
With compareRange.Parent 
    Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1"))) 
End With 
If compareRange Is Nothing Then Exit Function 
If stringsRange Is Nothing Then Set stringsRange = compareRange 
Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _ 
stringsRange.Column - compareRange.Column) 

For i = 1 To compareRange.Rows.Count 
    For j = 1 To compareRange.Columns.Count 
        If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then 
            If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then 
                ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j)) 
            End If 
        End If 
    Next j 
Next i 
ConcatIf = mid(ConcatIf, Len(Delimiter) + 1) 
End Function 

It also can't handle the "multiple numbers in one cell" as separate numbers. 它也不能将“一个单元格中的多个数字”作为单独的数字处理。

Is there a way to make a Concatenate UDF that "parses" the cells it's looking at to look for duplicates between the multiple numbers cells and the single numbers cells, and then output the result? 有没有办法让Concatenate UDF“解析”它正在查看的单元格,以查找多个数字单元格和单个数字单元格之间的重复,然后输出结果? Preferably allowing it to take a series of non-consecutive cells to work on (across different sheets). 优选地允许它采取一系列非连续的单元来处理(跨越不同的纸张)。

Sorry if the explanation is a bit convoluted, it's my first time asking for this kind of help. 对不起,如果解释有点复杂,这是我第一次要求这种帮助。 :x :X

Here's an example: 这是一个例子:

If I have cells with: 如果我有细胞:

  • 2,4,6 2,4,6
  • 2,6 2,6
  • 2 2
  • 4 4
  • 6 6
  • 6,8 6,8

I'd want to be able to simply get: 我希望能够简单地得到:

  • 2,4,6,8 2,4,6,8

Right now, instead, I'd get: 现在,相反,我会得到:

  • 2,4,6,2,6,6,8 2,4,6,2,6,6,8

Try the below. 试试下面的内容。 You can adapt it appropriately if you need to change the delimiter etc. I have documented what it is doing and why. 如果您需要更改分隔符等,您可以适当地调整它。我已经记录了它正在做什么以及为什么。

Example formula: =blah(A1:A7,A8,C9) (it can also be called from code) 示例公式: =blah(A1:A7,A8,C9) (也可以从代码中调用)

Example output: 2,4,6,8 输出示例: 2,4,6,8

Public Function Blah(ParamArray args()) As String
    'Declarations
    Dim uniqueParts As Collection
    Dim area As Range
    Dim arg, arr, ele, part
    Dim i As Long

    'Initialisations
    Set uniqueParts = New Collection

    'Enumerate through the arguments passed to this function
    For Each arg In args
        If TypeOf arg Is Range Then 'range so we need to enumerate its .Areas
            For Each area In arg.Areas
            arr = area.Value 'for large ranges it is greatly quicker to load the data at once rather than enumerating each cell in turn
                For Each ele In arr 'enumerate the array
                     addParts CStr(ele), uniqueParts 'Call our sub to parse the data
                Next ele
            Next area
        ElseIf VarType(arg) > vbArray Then 'an array has been passed in
            For Each ele In arg 'enumerate the array
                addParts CStr(ele), uniqueParts 'Call our sub to parse the data
            Next ele
        Else 'assume can be validly converted to a string. If it cannot then it will fail fast (as intended)
            addParts CStr(arg), uniqueParts 'Call our sub to parse the data
        End If
    Next arg

    'process our results
    If uniqueParts.Count > 0 Then
        ReDim arr(0 To uniqueParts.Count - 1)
        For i = 1 To uniqueParts.Count
            arr(i - 1) = uniqueParts(i)
        Next i
        'we now have an array of the unique parts, which we glue together using the Join function, and then return it
        Blah = Join(arr, ",")
    End If

End Function
'Sub to parse the data. In this case the sub splits the string and adds the split elements to a collection, ignoring duplicates
Private Sub addParts(partsString As String, ByRef outputC As Collection) 
'ByRef is unecessary but I use it to document that outputC must be instantiated
    Dim part
    For Each part In Split(partsString, ",")
        On Error Resume Next 'existing same key will raise an error, so we skip it and just carry on
        outputC.Add part, part
        On Error GoTo 0
    Next part
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel:连接单元格并删除重复项 - Excel: Concatenate Cells and Remove Duplicates Excel 2010:如何在多个单元格之间更改公式而不修改其输入值? - Excel 2010: How can I change formulas across multiple cells without modifying their input values? Excel 连接不同类型单元格的单元格值 - Excel concatenate cell values from cells of different types 如何对单元格中具有多个标准值的单元格进行计数 - How to count cells with multiple criteria values within the cell 删除 Excel 单元格中的重复项 - Remove duplicates within Excel cell 如何根据条件连接多个单元格中的值? - How to concatenate values in multiple cells based on a condition? 如何将EXCEL 2010中的选定单元格导出为HTML。 每次导出的像元范围可能不同吗? - How do I export selected cells in EXCEL 2010 as HTML. Cell range may be different on each export? 连接单元格并删除重复项 - Concatenate cells and remove duplicates 从excel vba中的单元格范围中删除重复项 - Remove Duplicates from range of cells in excel vba 如何根据另一个单元格的值自动为一系列单元格填充值? 有Excel等同于GoogleSheet的arrayformula吗? - How do I autofill a range of cells with values, based on another cell's value? Is there an Excel's equivalent of GoogleSheet's arrayformula?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM