Vba 查找并删除在=';逗号前的s和

Vba 查找并删除在=';逗号前的s和,vba,excel,Vba,Excel,我在电子表格的N列中有一个颜色列表,在每一行/单元格中,该列表的内容如下: 中蓝色=蓝色,浅蓝色=蓝色,中绿色=绿色,中橙色=橙色,中橙色=焦橙色,中灰色=不锈钢,深红色=焦橙色 我试图查看每个单元格,找到class='s的所有实例,并比较class='s之后的字符串,直到下一个逗号(例如:它将查看“=ESP”),以查看该值是否在同一单元格中多次出现(如果相同的值在不同的单元格中,则可以)。如果该值在同一单元格中多次出现,我需要删除class='s之后的字符串,并将其替换为class='s之前的

我在电子表格的N列中有一个颜色列表,在每一行/单元格中,该列表的内容如下:

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

我试图查看每个单元格,找到class='s的所有实例,并比较class='s之后的字符串,直到下一个逗号(例如:它将查看“=ESP”),以查看该值是否在同一单元格中多次出现(如果相同的值在不同的单元格中,则可以)。如果该值在同一单元格中多次出现,我需要删除class='s之后的字符串,并将其替换为class='s之前的字符串。在完成所有这些之后,我还需要确保没有两个类似的值(“浅蓝色&中蓝色=浅蓝色”被认为是相同的)。因此,正确时,上面的字符串应如下所示(保留尾随逗号):

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

 '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

这里有一种不同的方法,使用
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

这里有一种不同的方法,使用
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的所有努力和帮助,我能够在他给我的基础上构建一个功能,并最终构建出一个适合我需要的功能。我会把这个贴在这里,以防别人需要

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
函数CleanColor(v)
尺寸范围、e、b、a、rv、sep、arrV
作为字符串的Dim-ex
arr=拆分(v,“,”)
'在每对值上循环
对于arr中的每个e
'使用等号作为分隔符进一步拆分值
arrV=拆分(e,“=”)
'如果有空格并将alias设置为b,则从alias中修剪空格
b=微调(arrV(0))
'查看数组边界,如果有超过1个插槽(插槽0),则有一个=-分隔的对
如果UBound(arrV)>0,则
'是一对由=-分隔的值。。。
a=配平(arrV(1))
'计算“after”在整个v字符串中出现的次数
将计数设置为整数
计数=(Len(v)-Len(工作表函数)替换(v,Chr(61)和a,“”))/Len(Chr(61)和a)
“以前见过“之后”吗?
如果仪表(ex、Chr(0)和a&Chr(0))大于0或计数大于1,则
如果b为“其他”,则
a=b'已看到,在“=”之前”之后指定
其他的
后藤
如果结束
其他的
ex=ex&Chr(0)和a&Chr(0)
如果结束
rv=rv&sep&b&“=”&a
其他的
'在此处处理单个“b”值。。。。
a=e
'以前见过单一值吗?
如果InStr(ex,Chr(0)&a&Chr(0))>0,则
ex=ex'已看到,不要添加到字符串
其他的
ex=ex&Chr(0)和a&Chr(0)
rv=rv和sep&b
如果结束
“rv=rv和sep&b
如果结束
sep=“,”分隔符现在是逗号。。。
完:下一个e
CleanColor=rv
端函数

再次感谢Tim Williams的帮助

多亏@Tim Williams的努力和帮助,我才得以在他给我的基础上进行构建,并最终构建了一个适合我需要的函数。我会把这个贴在这里,以防别人需要

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
函数CleanColor(v)
尺寸范围、e、b、a、rv、sep、arrV
作为字符串的Dim-ex
arr=拆分(v,“,”)
'在每对值上循环
对于arr中的每个e
'使用等号作为分隔符进一步拆分值
arrV=拆分(e,“=”)
'如果有空格并将alias设置为b,则从alias中修剪空格
b=微调(arrV(0))
'查看数组边界,如果有超过1个插槽(插槽0),则有一个=-分隔的对
如果UBound(arrV)>0,则
'是一对由=-分隔的值。。。
a=配平(arrV(1))
'计算“after”在整个v字符串中出现的次数
将计数设置为整数
计数=(Len(v)-Len(工作表函数)替换(v,Chr(61)和a,“”))/Len(Chr(61)和a)
“以前见过“之后”吗?
如果仪表(ex、Chr(0)和a&Chr(0))大于0或计数大于1,则
如果b为“其他”,则
a=b'已看到,在“=”之前”之后指定
其他的
后藤
如果结束
其他的
ex=ex&Chr(0)和a&Chr(0)
如果结束
rv=rv&sep&b&“=”&a
其他的
'在此处处理单个“b”值。。。。
a=e
'以前见过单一值吗?
如果InStr(ex,Chr(0)&a&Chr(0))>0,则
ex=ex'已看到,不要添加到字符串
其他的
ex=ex&Chr(0)和a&Chr(0)
rv=rv和sep&b
如果结束
“rv=rv和sep&b
如果结束
sep=“,”分隔符现在是逗号。。。
完:下一个e
CleanColor=rv
端函数

再次感谢Tim Williams的帮助

从哪里得到错误,哪一行?'If(Application.WorksheetFunction.CountIf(InStr(ActiveSheet.Range(“N”&N).Value,umid(右(ActiveSheet.Range(“N”&N).Value,commaPos),左(ActiveSheet.Range(“N”&N).Value,equalPos),“>1”))然后“它似乎指向该行中的CountIf,错误在哪里,哪一行?”If(Application.WorksheetFunction.CountIf(InStr(ActiveSheet.Range(“N”&N).Value,uMID(右(ActiveSheet.Range(“N”&N).Value,commaPos),左(ActiveSheet.Range(“N”&N).Value,equalPos),“>1”)然后,“它似乎指向伯爵,如果在那一行,这就像一个魅力。你是最棒的!真不敢相信我花了这么多时间思考这么复杂的方法。我现在觉得自己像个新手。非常感谢。实际上,只有一个问题。查看stri时,它似乎给出了一个“#VALUE!”错误