我的电子表格的第N列中有一个颜色列表,每个行/单元格中的列表看起来都是这样的:
中等蓝色=蓝色,浅蓝色=中等绿色=绿色,中等橙色=橙色,中等橙色=烧焦橙色,中等灰色=不锈钢,深红色=橙色烧焦
我正在尝试查看每个单元格,查找='s的所有实例并比较='s之后的字符串直到下一个逗号(例如:它会看“= ESP”)来查看是否有此值在同一个单元格中多次出现(如果相同的值在不同的单元格中,则可以)。如果值在同一个单元格中多次出现,我需要在= s之后删除字符串,并将其替换为=之前的字符串。完成所有这些后,我还需要确保没有两个相似的值(“Light Blue& Medium Blue = Light Blue”被认为是相同的)。因此,上面的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':类型不匹配”错误。此外,我很确定如果这确实有效,它仍然不会捕获字符串末尾的值,因为在last ='s之后没有另一个逗号可以找到。
修改的
我的功能
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
答案 0 :(得分:2)
这是使用Split()
编辑:添加了检测单个值vs = -separated pairs
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
答案 1 :(得分:0)
感谢@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
再次感谢蒂姆·威廉姆斯的帮助!