过滤excel表以获取唯一集

时间:2016-03-16 21:20:15

标签: sql excel filter excel-formula business-intelligence

我有一个excel表,看起来像这样:

Row  Name 
 1   uniqueName001_vid1.mpg
 2   uniqueName001.mpg
 3   uniqueName002_vid1.mpg
 4   uniqueName002_vid2.mpg
 5   uniqueName002.mpg

我试图弄清楚如何在表格中识别和标记(给出一个唯一的ID)集合,其中包含相同的 uniqueName 。例如,Row的1和2将是一组,而Row的3,4和5将是另一组。

我理想的结果是:

Row  Name                     UID
 1   uniqueName001_vid1.mpg   SET1
 2   uniqueName001.mpg        SET1
 3   uniqueName002_vid1.mpg   SET2
 4   uniqueName002_vid2.mpg   SET2
 5   uniqueName002.mpg        SET2

我可以在excel中运行SQL查询,如果这是比excel公式更好的选择。

非常感谢任何建议!

2 个答案:

答案 0 :(得分:1)

如果一切都以uniqueNameXXX开头而不是

Row Name                    UniqueName      Unique#             UID
1   uniqueName001_vid1.mpg  =LEFT(F4;13)    =IF(G3<>G4;H3+1;H3) ="UID"&H4

如果没有,则应该定义如何获取uniqueName

答案 1 :(得分:1)

您可以使用VBA执行该任务。

我为你做了一个小工具。照顾可编辑的部分 根据声明。

此工具会侦听数字 - 意思是,我希望您的模式始终与您在问题中所写的相同。

告诉我这是否有帮助:

Sub ExtractIdFromString()

    Dim strYourColumn As String
    Dim intYourStartRow As Integer
    Dim intYourLengthOfId As Integer
    Dim strYourSetColumn As String
    Dim strYourPrefix As String
    Dim strString As String
    Dim intStringLength As Integer
    Dim intStringDigitPosition As Integer
    Dim intParserPosition As Integer
    Dim strParser As String
    Dim i As Integer
    Dim strUniqueString As String
    Dim rngCell As Range
    Dim rngSetCell As Range
    Dim strIndex As String
    Dim lngCounter As Long


    ''''editable values''''
    strYourColumn = "B"    'Your name column, must be alphabethical
    intYourStartRow = 1    'Startrow of your block, must not be 0
    intYourLengthOfId = 3  'The amount of digits in your ID, must be > 1
    strYourSetColumn = "C"    'The column, where the ID will be inserted, must be numerical (use A = 1, Z = 26)
    strYourPrefix = "SET"   'Prefix of your set's ID
    ''''end of editable values''''




    'Set the format of the ID column to text
    Range(strYourColumn & ":" & strYourColumn).NumberFormat = "@"

    'traverse through the names column
    For Each rngCell In Range(strYourColumn & ":" & strYourColumn)

        'initialize / reset parser
        intParserPosition = 1

        'get the actual string to value
        strString = rngCell.Value

        'End loop on empty cell
        If strString = "" Then
            GoTo massRename
        End If

        'get the string's length
        intStringLength = Len(strString)

        'parse through the string
        For intStringDigitPosition = 1 To intStringLength Step 1

            'end loop if the string is parsed without a result
            If intParserPosition > intStringLength Then
                Exit For
            End If

            'get single digit of the string
            strParser = Mid(strString, intParserPosition, 1)

            'listen on numbers
            If IsNumeric(strParser) Then

                'traverse through the expected ID slots
                For i = intParserPosition To intParserPosition + intYourLengthOfId - 1 Step 1

                    'listen for non numerical chars in the expected ID
                    If Not IsNumeric(Mid(strString, i, 1)) Then

                        'allow your titles to include numbers
                        GoTo SkipSingleNumerics

                    End If

                Next

                'get the unique prototype of the string
                strUniqueString = Mid(strString, 1, intParserPosition + intYourLengthOfId - 1)                    

                'write the unique name in a specified column
                Range(strYourSetColumn & rngCell.Row).Value = strUniqueString

            End If

'Skip numbers in the string, that dont dont match the ID pattern (optional)
SkipSingleNumerics:

            'traverse trough the word
            intParserPosition = intParserPosition + 1

        Next
    Next

'Rename and index equal values
massRename:

    lngCounter = 1
    'traverse through the set list
    For Each rngSetCell In Range(strYourSetColumn & ":" & strYourSetColumn)

        'end condition
        If rngSetCell.Value = "" Then
            Exit For
        End If

        'store value in variable to save it from overwriting
        strIndex = rngSetCell.Value

        'start another traversal instance
        For Each rngCell In Range(strYourSetColumn & ":" & strYourSetColumn)

            'end condition
            If rngCell.Value = "" Then
                Exit For
            End If

            'listen if both instances match
            If strIndex = rngCell.Value Then

                'rename the value
                rngCell.Value = strYourPrefix & lngCounter

            End If
        Next

        'increase unique counter
        lngCounter = lngCounter + 1
    Next
End Sub

在Excel 2010中测试