I have a list of colors in column N of my spreadsheet and in each row/cell the list looks something along the lines of:
Medium Blue=Blue,Light Blue=Blue,Medium Green=Green,Medium Orange=Orange,Medium Orange=Burnt Orange,Medium Gray=Stainless,Dark Red=Burnt Orange
I'm trying to look through each cell, find all instances of ='s and compare the string after ='s until the next comma (ex: it would look at "=ESP,") to see if this value occurs multiple times throughout the same cell (it's ok if the same values are in different cells). If the value occurs multiple times within the same cell I need to remove the string after the ='s and replace it with what's before the ='s. After all that is done, I also need to make sure that there are no two like values ("Light Blue & Medium Blue=Light Blue" are considered the same). So, the above String should look like this when correct (Leaving the trailing comma):
Medium Blue=Blue,Light Blue=Light Blue,Medium Green=Green,Medium Orange=Orange,Medium Orange=Burnt Orange,Medium Gray=Stainless,Dark Red=Dark Red
'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
I keep getting a "Run-time error '13': Type mismatch" Error. Also, I'm pretty sure if this did work it still wouldn't catch values at the end of the string since there is not another comma to find after the last ='s.
edit
My 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