Hi Marco,
You can try something like this (off the top of my head):
Public Function Example2(
<ExcelArgument(Name:="Value1", Description:="Value 1", AllowReference:=True)> value1 As Object,
<ExcelArgument(Name:="Value2", Description:="Value 2", AllowReference:=True)> value2 As Object
) As Integer
Now the argument values will be either a primitive value, or an object of type ExcelReference, which you can use in C API calls, or even convert to a COM Range using code like
Function ReferenceToRange(xlInput As Object) As Range
Dim reference As ExcelReference = xlInput ' Will throw some Exception if not valid, which will be returned as #VALUE
Dim app As Application = ExcelDnaUtil.Application
Dim sheetName As String = XlCall.Excel(XlCall.xlSheetNm, reference)
Dim index As Integer = sheetName.LastIndexOf("]")
sheetName = sheetName.Substring(index + 1)
Dim ws As Worksheet = app.Sheets(sheetName)
Dim target As Range = app.Range(ws.Cells(reference.RowFirst + 1, reference.ColumnFirst + 1),
ws.Cells(reference.RowLast + 1, reference.ColumnLast + 1))
For iInnerRef As Long = 1 To reference.InnerReferences.Count - 1
Dim innerRef As ExcelReference = reference.InnerReferences(iInnerRef)
Dim innerTarget As Range = app.Range(ws.Cells(innerRef.RowFirst + 1, innerRef.ColumnFirst + 1),
ws.Cells(innerRef.RowLast + 1, innerRef.ColumnLast + 1))
target = app.Union(target, innerTarget)
Next
Return target
End Function
-Govert
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/0990c043-562a-4c5a-8e86-520104886e32n%40googlegroups.com.
Hi Marco,
I think either would work, but I don’t really suggest putting the code into the .dna file anymore.
It’s much better if it is a normal pre-compiled project.
For now, the code in the .dan file is still supported, but some time in the future that will no longer be.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/1f100f23-8c36-444f-8eb4-114633a82379n%40googlegroups.com.
Hi Marco,
Are you using Visual Studio?
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/8c625a74-febf-41ca-a4f2-536b477bf626n%40googlegroups.com.
OK, I think you also need to add the “ExcelDna.Interop” NuGet package to your project, to get the interop assemblies where Range etc. are defined.
Then in your function you need to add the AllowReference:=True parts to the attributes, else you’ll never get passed the ExcelReference objects.
Finally you need to decide what to do with the Range – if you try to write to the range it will fail, of course.
But are you using the address or something from the range?
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/f62f9b92-14d1-43cf-90ed-5078531b2436n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/76cc2550-7311-437b-8cb2-15d6aa403aean%40googlegroups.com.
Ok, so if you just want a function that gets inputs and returns the result to the sheet, then you don’t need the AllowReference:=True or any of the COM stuff.
For example, this function takes two input values and returns the first value to the cell.
Imports ExcelDna.Integration
Public Module Functions
Public Function Example2(
<ExcelArgument(Name:="Value1", Description:="Value 1")> value1 As Object,
<ExcelArgument(Name:="Value2", Description:="Value 2")> value2 As Object
) As Object
Example2 = value1
End Function
End Module
Note that I have made some changes:
Example2 = value1
Return value1
I prefer the second way where you say ‘Return’ because if you change the name of the function, things don’t break.
The first line is the old VBA style, where the function name is like a magic variable, that gets the return value.
I hope you can try it like this.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/29804547-ad18-4e79-b87a-56e106c9f18an%40googlegroups.com.
Hi Marco,
The most common problem is if the function is inside a Class instead of a Module.
Functions inside a class can be instance methods or “Shared” methods – for the Excel UDF it must be “Shared”.
All functions in a Module are automatically “Shared” so that’s normally easier.
To be registered with Excel it must either be a Public Function in a Public Module or a Public Shared Function in a Public Class.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/3aa7a27d-25c3-4f4a-9f5e-9c89bde7c5cen%40googlegroups.com.