In this Walkthrough we will use C# and Visual Studio 2012 to create a simple Excel UDF. You can use any other .NET language and development environment to achieve the same result in a similar way.

Create a C# class library project

  1. Open Visual Studio 2012

  2. Go to File->New->New Project

  3. Select .NET 4.5 as Target Framework

  4. Select a template for C# Class Library Project

  5. Select project location, enter CSharpUdfs as name and click OK

    VSProject

Implement User Defined Function

Create class for UDF

  1. Rename Class1 in your project into TestUdfs

  2. Add a static method to TestUdfs

    C# Copy imageCopy
    namespace CSharpUdfs
    {
        public class TestUdfs
        {
            public static double Add2Args(double x, double y)
            {
                return x + y;
            }
        }
    }

Access UDF in Excel

Expose UDF to Excel

  1. Build the project in Debug

  2. Open Excel and go to FCell Ribbon

    FCell Menu
  3. Click on Excel .NET AddIn, add CSharpUdfs.dll and save changes:

    CSharp Udf Source
  4. Click on Reload Excel AddIn, enter in Excel =Add2Args(2,3) and verify UDF works

    Add 2 Args
  5. Go back to C# code and modify your UDF

    C# Copy imageCopy
    namespace CSharpUdfs
    {
        public class TestUdfs
        {
            public static double Add2Args(double x, double y)
            {
                return x + y + 10;
            }
        }
    }
  6. Build the project

  7. Go to Excel, click on Reload Excel AddIn and verify that UDF has been refreshed:

    Add 2 Args Modified

Debug UDF in Visual Studio

Attach Debugger to Excel

  1. In Visual Studio go to Debug->Attach to Process and attach to Excel.exe

    Attach To Process
  2. Add a breakpoint in your C# UDF

    Breakpoint
  3. Add a new formula in Excel, e.g. =Add2Args(4,5) and verify that breakpoint was hit in Visual Studio:

    Breakpoint Hit