Create a named range

Jul 23, 2015 at 3:55 AM
Hi,

I would like to know how to create new named ranges via code. I need to create a few global named ranges when the add-in loads up. Is this possible?

Regards,
Ganesh
Coordinator
Jul 24, 2015 at 10:48 PM
Hi Ganash,

Yes - you can use either the C API or the COM object model in your add-in.
For this it's probably simpler to use the COM object model.

Add a reference to Microsoft.Office.Interop.Excel.dll, then your code might be something like:
using Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;

public class MyAddIn : IExcelAddIn
{
    public void AutoOpen()
    {
        Application xlApp = (Application)ExcelDnaUtil.Application;
        // Here your code looks like VBA< using the xlApp COM object.
         xlApp.ActiveWorkbook.Names.Add(...);
    }

    public void AutoClose() {}
}
-Govert