过滤Excel表格的独特集合

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

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

我想弄清楚如何识别和标记(给一个唯一的ID)在表中包含相同的唯一名称集。 例如,行1和行2将是一组,而行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公式更好的select。

任何build议,非常感谢!

如果一切都以uniqueNameXXX开头,那么比较容易

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

如果没有,比你应该定义如何获得uniqueName

您可以使用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中进行testing