将值存储在一个单元格中

时间:2015-06-17 08:47:03

标签: excel excel-vba excel-formula excel-2010 vba

我在excel中有一列,其中有值列表。

我想要做的是我想从该列中删除重复值,并希望将所有唯一值存储在一个单元格中 例如删除重复项后,如果有下面的值列表 OPF OIP 猫头鹰 OVL 然后我想在一个单元格中存储上述细节,即OPF,OIP,OWL,OVL

请帮助...如果有任何vba代码或excel公式

将会很好

Sample

2 个答案:

答案 0 :(得分:0)

感谢所有帮助...

我完成了自己的.....然而真的非常感谢...

Sub combine()
Dim mstr, mstr2 As String
Dim v As Range
Set v = Sheets("Data").Range("A1")
mstr = ""
mstr2 = ""
i = 0
Do
i = i + 1
v.Cells(i, 1).Select

If IsNumeric(v.Cells(i, 1)) = True Then
mstr = mstr + "'" + str(v.Cells(i, 1)) + "'  or "
mstr2 = mstr2 + "'" + str(v.Cells(i, 1)) + "'  , "
Else
mstr = mstr + "'" + v.Cells(i, 1) + "'  or "
mstr2 = mstr2 + "" + v.Cells(i, 1) + "  , "
End If

Loop While IsEmpty(v.Cells(i + 1)) = False
mstr = Left(mstr, Len(mstr) - 4)
mstr2 = Left(mstr2, Len(mstr2) - 4)
Sheets("Data").Range("C3") = mstr
Sheets("Data").Range("C4") = mstr2

End Sub

答案 1 :(得分:0)

您可以创建以下自定义功能:

Function UniqueInString(InputList As Range) As String

    Dim inputListArray() As String

    Dim cell As Range
    Dim size As Integer
    size = 0
    ReDim Preserve inputListArray(size)
    For Each cell In InputList.Cells
        If Not (UBound(Filter(inputListArray, cell.Value)) > -1) Then
            ReDim Preserve inputListArray(size)
            inputListArray(size) = cell.Value
            size = size + 1
        End If
    Next cell


    Dim counter As Integer
    Dim result As String
    result = ""
    For counter = 0 To UBound(inputListArray)
        result = result + inputListArray(counter) + ","
    Next counter
    result = Left(result, Len(result) - 1)
    UniqueInString = result
End Function

这将为您提供一个单元格中的唯一记录,而无需创建唯一记录列表。只需在包含所有记录的列表中调用此函数。请注意,如果有很多记录,我不会这么做。