Fill cells in Excel 2007 based on content of other cells in row

后端 未结 1 459
野性不改
野性不改 2021-01-26 16:16

I have an Excel 2007 spreadsheet populated with four columns (V1 V2, V3 V4) that contain values (integers [0-20]) separated by a comma. Values need not be continuous.

相关标签:
1条回答
  • 2021-01-26 16:43

    If each value can only appear once in each cell:

    Function IfAtLeast(rng As Range, num As Integer)
        Const SEP As String = ","
        Dim c As Range, d As Object, arr, i As Long, tmp, k
        Dim rv As String
        rv = ""
        Set d = CreateObject("scripting.dictionary")
        For Each c In rng
            arr = Split(c.Value, SEP)
            For i = LBound(arr) To UBound(arr)
                tmp = Trim(arr(i))
                If Not d.exists(tmp) Then d.Add tmp, 0
                d(tmp) = d(tmp) + 1
            Next i
        Next c
        For Each k In d.keys
            If d(k) >= num Then rv = rv & IIf(Len(rv) > 0, ",", "") & k
        Next k
        IfAtLeast = rv
    End Function
    

    Usage:

    =IfAtLeast(A2:D2,2)
    

    EDIT: if you're having problems trouble-shooting a UDF then it is often easier to debug the issue using a sub

    Sub Tester()
        debug.print IfAtLeast(Activesheet.Range("A2:D2"), 2)
    End Sub
    
    0 讨论(0)
提交回复
热议问题