Option Compare Text
Public Sub StripVariants()
Call StripVariant(Worksheets("Sheet1").Range("C:C"), Worksheets("Sheet1").Range("F:F"))
Call StripVariant(Worksheets("Sheet1").Range("D:D"), Worksheets("Sheet1").Range("F:F"))
End Sub
Public Sub StripVariant(ByVal rngVariant As Excel.Range, ByVal rngText As Excel.Range)
Dim arrVariant As Variant
Dim arrText As Variant
Dim lngLastRow As Long
Dim lngRow As Long
Dim intCol As Integer
Dim intPos As Integer
intCol = rngVariant.Column
lngLastRow = rngVariant.Parent.Columns(intCol).Find(What:="*", After:=Cells(1, intCol), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
LookAt:=xlPart, LookIn:=xlValues).Row
arrVariant = rngVariant.Cells(1, 1).Offset(1, 0).Resize(lngLastRow - 1, 1).Value
arrText = rngText.Cells(1, 1).Offset(1, 0).Resize(lngLastRow - 1, 1).Value
For lngRow = LBound(arrVariant) To UBound(arrVariant)
If Len(arrVariant(lngRow, 1)) > 0 Then
intPos = InStr(1, arrText(lngRow, 1), Trim(arrVariant(lngRow, 1)))
If intPos > 0 Then
arrText(lngRow, 1) = Left(arrText(lngRow, 1), intPos - 1) _
& Mid(arrText(lngRow, 1), intPos + Len(arrVariant(lngRow, 1)))
If Mid(arrText(lngRow, 1), intPos - 1, 2) = " " Then
arrText(lngRow, 1) = Left(arrText(lngRow, 1), intPos - 1) _
& Mid(arrText(lngRow, 1), intPos + 1)
ElseIf Mid(arrText(lngRow, 1), intPos - 1, 3) = " , " Then
arrText(lngRow, 1) = Left(arrText(lngRow, 1), intPos - 2) _
& Mid(arrText(lngRow, 1), intPos + 1)
End If
End If
End If
Next lngRow
rngText.Cells(2, 1).Resize(UBound(arrText), 1).Value = arrText
End Sub
To use it, go to the VBA IDE (alt-F11), insert a new module and paste in the code. You may need to change the StripVariants module to point to the right sheets and columns in your production worksheet, but it works in the example. With your cursor in the StripVariants procedure, just press F5 to run it (for testing, I'm sure you'll want a button somewhere later).
Bookmarks