In Excel 2010, how can I remove duplicates and merge values ​​within a cell range that includes multiple value cells?

I made a document in Excel 2010, but the functionality that I hope to get from it seems impossible (at least not with Excel functions by default), and I don't know enough about VB programming for make my own UDF. (I actually use the one I found on the Internet that does part of what I want, but does not meet all my needs.)

Let me break it:

  • I have several sheets with field groups where users can add numbers (some will be empty, some will contain one number, some will contain several comma-separated numbers)

  • I have a “Browse” sheet in which I want to combine these numbers (and remove any duplicates) in several different sections (looking only at certain groups of fields).

I found ConcatIf UDF that works well enough for this, however it cannot handle undeveloped cells for concatenation (for example, I want to merge and remove duplicates from D30, G30, J30 and M30 cells together) (Here 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 cannot process "multiple numbers in one cell" as separate numbers.

Is there a way to make UCF Concatenate, which “analyzes” the cells it is looking at, look for duplicates between multiple number cells and single number cells, and then output the result? It is preferable to allow it to accept a number of non-consecutive cells for work (through different sheets).

, , , .:

:

:

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

:

  • 2,4,6,8

:

  • 2,4,6,2,6,6,8
+4
1

. , .. , .

: =blah(A1:A7,A8,C9) ( )

: 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
+2

Source: https://habr.com/ru/post/1531596/


All Articles