Hey,
Does anyone know how to stop a UDF from recalculating unless its target cells change? My UDF below recalculates constantly. Thanks!
Does anyone know how to stop a UDF from recalculating unless its target cells change? My UDF below recalculates constantly. Thanks!
Code:
Function Custom_SplitandRemove3(InitialRange As String, RemoveMatchesFromThisRange As String)
On Error GoTo Troubleshooter1
'Accounts for handling if either string is blank
If InitialRange = Empty Or InitialRange = "" Then 'Or InitialRange = 0
GoTo Troubleshooter_EmptyFirstValue
End If
If RemoveMatchesFromThisRange = Empty Or Trim(RemoveMatchesFromThisRange) = "" Then
GoTo Troubleshooter_EmptySecondValue
End If
'Setup
Z = 0
b = 0
n = 0
Dim NewArray() As String
ReDim NewArray(0 To 5000)
'Splits values by ;
SplitValuesInitialRange = Split(InitialRange, ";")
SplitValuesSecondaryRange = Split(RemoveMatchesFromThisRange, ";")
'Checks to see if there are any matching values between arrays, if there are matches, this removes them
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
If n > 0 Then
ReDim Preserve NewArray(0 To n - 1)
End If
'Builds new array with removed duplicates back into a string
For b = 0 To n - 1
NewString = NewArray(b) & NewString
Next b
'Clears a few variables (not needed)
b = 0
n = 0
Erase NewArray
'If string one matched string two, nothing is left so put "", otherwise input the New String
If NewString = Empty Then
Custom_SplitandRemove = ""
Else
Custom_SplitandRemove = NewString
End If
NewString = Empty
Exit Function
'If there is an error, show it with this
Troubleshooter1:
MsgBox ("Something went wrong" & vbCrLf & Err.Description & vbCrLf & Err.Number & vbCrLf & Err.Source & vbCrLf & Error & vbCrLf & vbObjectError & vbCrLf & InitialAddress & " " & RemoveMatchesAddress)
Exit Function
'Handles Empty First or Second Strings
Troubleshooter_EmptyFirstValue:
Custom_SplitandRemove = ""
Exit Function
Troubleshooter_EmptySecondValue:
Custom_SplitandRemove = Trim(InitialRange)
Exit Function
End Function