Not at all. VBA Evaluate does all the real work in the Excel thread. Very inefficient. Simply use WorksheetFunction.
Rich (BB code):
Function cosSim(a As Variant, b As Variant) As Variant
Dim wf As Object
Set wf = WorksheetFunction
On Error Resume Next
cosSim = wf.SumProduct(a, b) / Sqr(wf.SumSq(a)) / Sqr(wf.SumSq(b))
If Err <> 0 Then cosSim = CVErr(xlErrValue)
Be sure to put the function into a normal VBA module; that is, in VBA, click Insert > Module.
Examples of usage:
I did not respond earlier because I cannot see any reason to implement this in VBA, unless you need to call cosSim() from existing VBA code. Otherwise, it is much more efficient to use the Excel formula that I provided earlier.
Function CosSimS(x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Variant
Dim dMag As Double
Const D2R = 180# / 3.141592654
dMag = (x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)
If dMag Then
CosSimS = (x1 * x2 + y1 * y2) / Sqr(dMag) * D2R
CosSimS = CVErr(xlErrDiv0)