Hey there,
I've created a custom function which compares two text strings, and removes any duplicates. For instance
String 1 = "Bob; Sally; Tom; Beth;"
String 2 = "Sally; Beth;"
Resulting String = "Bob; Tom;"
I have two issues. One is that my function is being recalculated needlessly. For instance, if I create a new worksheet and enter information into a cell it hangs as this code is run.
The second issue is that as I reference this formula over 6,000 times, doing something simple like removing a column can take quite some time. Coupled with the above problem that doesn't bode well.
Could you please help me troubleshoot the auto-recalculation and/or increase the codes efficiency? This is my first UDF. Thanks!
Code below -
I've created a custom function which compares two text strings, and removes any duplicates. For instance
String 1 = "Bob; Sally; Tom; Beth;"
String 2 = "Sally; Beth;"
Resulting String = "Bob; Tom;"
I have two issues. One is that my function is being recalculated needlessly. For instance, if I create a new worksheet and enter information into a cell it hangs as this code is run.
The second issue is that as I reference this formula over 6,000 times, doing something simple like removing a column can take quite some time. Coupled with the above problem that doesn't bode well.
Could you please help me troubleshoot the auto-recalculation and/or increase the codes efficiency? This is my first UDF. Thanks!
Code below -
Code:
Function Custom_SplitandRemove(InitialRange, RemoveMatchesFromThisRange)
n = 0
Dim NewArray() As Variant
ReDim NewArray(0 To 5000) ;The 5000 is arbitrary, could be lowered. Most strings have <10 items.
'Splits the values by delimiter
SplitValuesInitialRange = Split(InitialRange, ";")
SplitValuesSecondaryRange = Split(RemoveMatchesFromThisRange, ";")
'For each initial value, checks for a match from secondary range. If there is no match, adds it to an array.
For Each InitialValue In SplitValuesInitialRange
If WorksheetFunction.Trim(InitialValue) <> "" Then
For i = 0 To UBound(SplitValuesSecondaryRange)
If WorksheetFunction.Trim(InitialValue) = WorksheetFunction.Trim(SplitValuesSecondaryRange(i)) Then
TheresAMatch = True
End If
Next i
If TheresAMatch = False Then
NewArray(n) = WorksheetFunction.Trim(InitialValue) & "; "
n = n + 1
End If
TheresAMatch = Empty
End If
Next InitialValue
ReDim Preserve NewArray(0 To n - 1)
'Puts array into string so I can put the result in a cell.
For b = 0 To n - 1
NewString = NewArray(b) & NewString
Next b
Custom_SplitandRemove = NewString
End Function