Hi,
Your two questions are very different, since the first concerns a
worksheet function and the second a macro.
For the worksheet function, your function will return values into the
calling cell - or into the cells of the calling range if you enter it
as an array formula. In Excel's calculation model your worksheet
function is not supposed to change other parts of the sheet, and Excel
tried pretty hard to prevent it. If you really want to populate parts
of the sheet that did not call your function, there are some hacks to
do this, but it is not recommended, and is certainly not something I
would recommend unless you have a lot of experience building add-ins
for Excel.
For the macro case, where you want to create some output (maybe based
on the current selection) and then put stuff into a new worksheet, you
are on much safer ground. When you click the menu, Excel is ready for
you to change stuff. There are two APIs available in this context:
1. The Automation COM interfaces, as you might know from VBA. The only
issue is how to get hold of the root Application object which is
conveniently always around in VBA. In your Excel-Dna code you call
ExcelDna.Integration.ExcelDnaUtil.Application to get the right
Application object. From there you can call
xlApp.ActiveWorkbook.Worksheets.Add(...) etc. This will be easier if
you are familiar with VBA. You can use
VB.NET or the dynamic keyword
in C# 4 for late binding, or do early binding if you reference the
Excel interop assembly.
2. The C SDK interface, which you access through the
ExcelDna.Integration.XlCall.Excel(...) helper function. This will be
easier if you are familiar with the C API - only you don't have to
deal with XLOPERs and funny data type wrappers. To insert a new sheet
you can try XlCall.Excel(XlCall.xlcWorkbookInsert, 1). Then to
populate the data use the ExcelReference type to set up a reference,
and SetValue(myValues) to set the contents. I put an example of
reading data from the selection and pasting into another sheet here:
http://stackoverflow.com/questions/3840270/fastest-way-to-interface-between-live-unsaved-excel-data-and-c-objects
I hope this gives you some ideas.
Govert