查找并移除在“=”和“逗号”之前出现的多个单词

我在电子表格的第N列有一个颜色列表,在每一行/单元格中,列表看起来像是:

中等蓝色=蓝色,浅蓝色=蓝色,中等绿色=绿色,中等橙色=橙色,中等橙色=烧焦的橙色,中灰色=不锈钢,深红色=烧焦的橙色

我试图查看每个单元格,find=的所有实例,并比较=之后的string,直到下一个逗号(例如:它将看“= ESP,”),看看这个值是否出现多次在同一个单元格中(如果相同的值在不同的单元格中,也可以)。 如果该值在同一个单元格中出现多次,则需要在=之后移除该string,并将其replace为=之前的string。 在完成所有工作之后,我还需要确保没有两个类似的值(“浅蓝色和中等蓝色=浅蓝色”被认为是相同的)。 所以,上面的string在正确的时候应该看起来像这样(留下尾随的逗号):

中等蓝色=蓝色,浅蓝色=浅蓝色,中等绿色=绿色,中等橙色=橙色,中等橙色=烧橙,中灰=不锈钢,深红色=深红色

'This is to figure out how many times to loop through a cell (Number of occurances 'of "=" in a given cell 'LEN(N2)-LEN(SUBSTITUTE(N2,"=","")) Dim endRange As Integer Dim equalCount As Integer endRange = ActiveSheet.Cells(Rows.Count, "N").End(xlUp).Row 'Loop through each row in the column For N = 2 To endRange 'Skip over a row if there is nothing in the cell If ActiveSheet.Range("N" & N).Value <> "" Then 'Counts how many ='s there are in each cell equalCount = Len(ActiveSheet.Range("N" & N).Value) - Len(Application.WorksheetFunction.Substitute(ActiveSheet.Range("N" & N).Value, "=", "")) 'Loops through a cell once for every ='s For c = 1 To equalCount Dim commaPos As Integer Dim equalPos As Integer 'Find the next comma & that's immediately after the particular ='s commaPos = FindN(",", ActiveSheet.Range("N" & N).Value, (c)) equalPos = FindN("=", ActiveSheet.Range("N" & N).Value, (c)) 'Search the cell to see how many instances of the value between the ='s and , If (Application.WorksheetFunction.CountIf(InStr(ActiveSheet.Range("N" & N).Value, _ Mid(Right(ActiveSheet.Range("N" & N).Value, commaPos), Left(ActiveSheet.Range("N" & N).Value, equalPos), _ equalPos - commaPos)), ">1")) Then MsgBox ("Found a Duplicate!") End If Next c End If Next N End Sub 

我不断收到“运行时错误13”:types不匹配“错误。 另外,我敢肯定,如果这工作,它仍然不会在string的末尾捕获值,因为在last =之后没有find另一个逗号。

编辑

我的function

  Function FindN(sFindWhat As String, _ sInputString As String, N As Integer) As Integer Dim J As Integer Application.Volatile FindN = 0 For J = 1 To N FindN = InStr(FindN + 1, sInputString, sFindWhat) If FindN = 0 Then Exit For Next End Function 

这是使用Split()的另一种方法

编辑 :添加检测单个值vs =分离对

 Function FixItUp(v) Dim arr, e, b, a, rv, sep, arrV Dim ex As String arr = Split(v, ",") 'loop over each pair of values For Each e In arr arrV = Split(e, "=") b = Trim(arrV(0)) If UBound(arrV)>0 Then 'is a =-separated pair of values... a = Trim(arrV(1)) 'seen the "after" before? If InStr(ex, Chr(0) & a & Chr(0)) > 0 Then a = b 'seen already, assign "after" = "before" Else ex = ex & Chr(0) & a & Chr(0) End If rv = rv & sep & b & "=" & a Else 'deal with the single "b" value here.... End If sep = "," 'separator is now a comma... Next e FixItUp = rv End Function 

感谢@Tim Williams的所有努力和帮助,我已经能够build立起他给我的东西,并最终构build了一个适合我需求的function。 我会在这里发布,以防其他人需要它

 Function CleanColor(v) Dim arr, e, b, a, rv, sep, arrV Dim ex As String arr = Split(v, ",") 'loop over each pair of values For Each e In arr 'Split up values further by using equals as delimiter arrV = Split(e, "=") 'Trimming space off alias if there is a space and setting alias to b b = Trim(arrV(0)) 'Looking at array bounds and if there more than 1 slot (slot 0) then we have an =-separated pair If UBound(arrV) > 0 Then 'is a =-separated pair of values... a = Trim(arrV(1)) 'count how many times the "after" appears in the entire v string Dim count As Integer count = (Len(v) - Len(WorksheetFunction.Substitute(v, Chr(61) & a, ""))) / Len(Chr(61) & a) 'seen the "after" before? If InStr(ex, Chr(0) & a & Chr(0)) > 0 Or count > 1 Then If b <> "Other" Then a = b 'seen already, assign "after" = "before" Else GoTo endFor End If Else ex = ex & Chr(0) & a & Chr(0) End If rv = rv & sep & b & "=" & a Else 'deal with the single "b" value here.... a = e 'seen the single value before? If InStr(ex, Chr(0) & a & Chr(0)) > 0 Then ex = ex 'seen already, don't add to string Else ex = ex & Chr(0) & a & Chr(0) rv = rv & sep & b End If 'rv = rv & sep & b End If sep = "," 'separator is now a comma... endFor: Next e CleanColor = rv End Function 

再次感谢Tim Williams的帮助!