Creating a User Defined Function in C#

UPDATE: See latest post:

UPDATE: See POC example project:

…and replacing a formula at runtime. Open-mouthed smile

In this posting, I am going to tackle two things at once:

  • The first is writing an Excel User Defined Function (UDF) using an Add-in via C#.
  • The second is a far more difficult proposition of having the function replace the formula in the evaluated cell.

I was working at a customer who has some legacy UDF’s still in VBA Add-ins (XLA or XLAM files). I strongly advise clients against mixing and matching VSTO and VBA. So, I showed them how to move their UDF’s completely to a C# add-in.

Additionally, in evaluating their issue, they want to replace their legacy UDF with a simple placeholder function that will repeat the value given. So, it boils down to taking the existing formula while it is being evaluated and replacing it. Excel will not let you do this… easily. Hot smile

The following code demonstrated how to define a UDF in C# and then to replace a formula while it is being evaluated:

public interface IFunctions
string UDF_Replace(object a);   // new interface
string UDF(object a, object b); // legacy interface
/// This is how COM will see the class for the functions
/// using the interface defined above. This has to use
/// the IDTExtensibility interface as well. You cannot
/// use VSTO as it short-circuts the extensibility for
/// you automatically and there is no way to register
/// it to be seen in this way…
public class Connect : Object, Extensibility.IDTExtensibility2, IFunctions
public Connect() { } // constructor
// These 6 methods are required for the IDTExtensibility2 interface
public void OnBeginShutdown(ref System.Array a) { }
public void OnStartupComplete(ref System.Array a) { }
public void OnAddInsUpdate(ref System.Array a) { }
public void OnDisconnection(Extensibility.ext_DisconnectMode e,
ref System.Array a) { }
public void OnConnection(object application,
Extensibility.ext_ConnectMode e,
object oo, ref System.Array a) { }
// These next three methods are registering the DLL with COM
public static void RegisterFunction(Type type)
public static void UnregisterFunction(Type type)
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type), false);
private static string GetSubKeyName(Type type)
string s = @”CLSID\{“ + type.GUID.ToString().ToUpper() +
return s;

/// This function is the new user defined function
///<param name=”a”></param>
public string UDF_Replace(object a)
// >> Do your work here <<
return a.ToString();

/// This function is the legacy user defined function
///<param name=”a”></param>
///<param name=”b”></param>
public string UDF(object a, object b)
// get reference to the cell where the function
// is being evaluated
Excel.Range thisRange = (Excel.Range)
// get the value in the cell…
string ret = thisRange.Value2.ToString();
// You cannot change a formula while you are inside the
// evaluation of that formula. But you can spawn off a
// thread so that this will get processed after this
// function exits…
new Thread(new ThreadStart(delegate
// replace the formula
thisRange.FormulaR1C1Local = “=UDF_Replace(“ + ret + “)”;
return ret; // return


To build this, you simply need to create a Shared Add-in Project in Visual Studio, like this:


Once you place the code above in place, you might want to get your own GUID. Click Tools > Create GUID:


Leave a Reply