The following errors did not affect the functionality of exported functions (Excel 2013 / Visual Studio 2017 up to date). So the functions worked out anyway:

I guess this comes from usingRange directly in the signature of the exposed functions (which is intended by the use of this extension as per this example). As said, the functions work fine, however, the registration throws that error. I have compared the
- Is there a way to avoid this error when registering the add-in?
Although the functions work fine, it gives me the impression that I am doing something wrong but cannot tell what.
The file:
Option Explicit On
'Option Strict On
Imports Microsoft.Office.Interop.Excel
Imports ExcelDna.Integration
Public Module Globals
' connect the global Application to the Excel instance via ExcelDna
Public ReadOnly Property Application As Application
Get
Return ExcelDnaUtil.Application
End Get
End Property
End Module
Public Module ExtendedRange
<ExcelFunction(Category:="ExtendedRange", Description:="Checksif any cell in the Range has a validation")>
Public Function RngHasValidation(rnIn As Range) As Boolean
Dim rnAux As Range
On Error Resume Next
RngHasValidation = Not (RngWithValidation(rnIn) Is Nothing)
End Function
<ExcelFunction(Category:="ExtendedRange", Description:="Given a Range it returns the Range thereof that has validations")>
Public Function RngWithValidation(rnIn As Range) As Range
Dim rnOut As Range
If rnIn.Cells.Count = 1 Then
rnOut = Application.Intersect(rnIn.SpecialCells(XlCellType.xlCellTypeAllValidation), rnIn)
Else
rnOut = rnIn.SpecialCells(XlCellType.xlCellTypeAllValidation)
End If
On Error GoTo 0
RngWithValidation = rnOut
End Function
<ExcelFunction(Category:="ExtendedRange", Description:="Returns the NamedRange of a cell with validation list")>
Public Function RngStrValidationNamedRangeInRange(rnIn As Range) As String
Dim strName As String, nParts() As String, FormulaParts() As String, strFormula As String
Dim rnWithValidation As Range
Dim rnArea As Range, rnRow As Range, rnCell As Range
Dim blnFound As Boolean
On Error Resume Next
RngStrValidationNamedRangeInRange = vbNullString
If Not RngHasValidation(rnIn) Then Exit Function
strName = vbNullString
rnWithValidation = RngWithValidation(rnIn)
blnFound = False
For Each rnArea In rnIn.Areas
For Each rnRow In rnArea.Rows
For Each rnCell In rnRow.Columns
If RngHasValidation(rnCell) Then
If rnCell.Validation.Type = XlDVType.xlValidateList Then ' type = 3
strFormula = rnCell.Validation.Formula1
FormulaParts = Split(strFormula, "=")
If UBound(FormulaParts) < 1 Then
Exit Function ' too bad (the formula is broken)
Else
blnFound = True
strName = FormulaParts(1)
nParts = Split(strName, "!")
If UBound(nParts) > 0 Then strName = nParts(1)
Exit For
End If
End If
End If
Next
If blnFound Then Exit For
Next
If blnFound Then Exit For
Next
If Not blnFound Then Exit Function
RngStrValidationNamedRangeInRange = strName
End Function
End Module
Basic Excel-DNA configuration and usage
Before installing the Excel-DNA Registration I went through the basic examples, where you have to declare function parameters where could receive ExcelReference with <ExcelArgument(AllowReference:=True)>.
After some tries, the above worked just fine with Microsoft.Office.Interop.Excel types, such as Range by simply not exposing Range type in the function signature as so:
<ExcelFunction(Category:="ExtendedRange", Description:="Checks if any cell in the Range has a Formula", IsMacroType:=True)>
Public Function RngHasFormulas(<ExcelArgument(AllowReference:=True)> ByVal rngIn As Object) As Boolean
As explained here (by Patrick O'Beirne), in the body, I had to use the following helper to transform the ExcelReference to a Range:
Public Function ReferenceToRange(ByVal xlRef As ExcelReference) As Object
Dim cntRef As Long, strText As String, strAddress As String
strAddress = XlCall.Excel(XlCall.xlfReftext, xlRef.InnerReferences(0), True)
For cntRef = 1 To xlRef.InnerReferences.Count - 1
strText = XlCall.Excel(XlCall.xlfReftext, xlRef.InnerReferences(cntRef), True)
strAddress = strAddress & "," & Mid(strText, strText.LastIndexOf("!") + 2)
' +2 because IndexOf starts at 0
Next
ReferenceToRange = ExcelDnaUtil.Application.Range(strAddress)
End Function
Adding the Excel-DNA Registration extension
With the aim of avoiding such syntax in my VBA to vb.net migration, I decided to add this other extension. Then I followed the guideline how install the package in a Visual Basic project.
This is the dna file:
<DnaLibrary Name="MyAddIn Add-In" RuntimeVersion="v4.0">
<ExternalLibrary Path="MyAddIn.dll" ExplicitRegistration="true" ExplicitExports="true" LoadFromBytes="true" Pack="true" />
<Reference Path="ExcelDna.Registration.dll" Pack="true" />
<Reference Path="ExcelDna.Registration.VisualBasic.dll" Pack="true" />
</DnaLibrary>
Comparing with your Visual Basic example I saw that the package installation added some entries in the packages.config file that are not in the counterpart file of your example. So then I commented those two lines just to see if that would fix the registration issue (no luck in this; yet the exported functions kept working):
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ExcelDna.AddIn" version="0.34.6" targetFramework="net45" />
<package id="ExcelDna.Integration" version="0.34.6" targetFramework="net45" />
<package id="ExcelDna.Interop" version="14.0.1" targetFramework="net45" />
<!--package id="ExcelDna.Registration" version="0.34.7" targetFramework="net45" />
<package id="ExcelDna.Registration.VisualBasic" version="0.34.7" targetFramework="net45" /-->
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
</packages>
The error has been already reported at the top of this comment.
The following errors did not affect the functionality of exported functions (
Excel 2013/Visual Studio 2017up to date). So the functions worked out anyway:I guess this comes from using
Rangedirectly in the signature of the exposed functions (which is intended by the use of this extension as per this example). As said, the functions work fine, however, the registration throws that error. I have compared theAlthough the functions work fine, it gives me the impression that I am doing something wrong but cannot tell what.
The file:
Basic Excel-DNA configuration and usage
Before installing the
Excel-DNA RegistrationI went through the basic examples, where you have to declare function parameters where could receiveExcelReferencewith<ExcelArgument(AllowReference:=True)>.After some tries, the above worked just fine with
Microsoft.Office.Interop.Exceltypes, such asRangeby simply not exposingRangetype in the function signature as so:As explained here (by Patrick O'Beirne), in the body, I had to use the following helper to transform the
ExcelReferenceto aRange:Adding the Excel-DNA Registration extension
With the aim of avoiding such syntax in my
VBAtovb.netmigration, I decided to add this other extension. Then I followed the guideline how install the package in a Visual Basic project.This is the
dnafile:Comparing with your Visual Basic example I saw that the package installation added some entries in the
packages.configfile that are not in the counterpart file of your example. So then I commented those two lines just to see if that would fix the registration issue (no luck in this; yet the exported functions kept working):The error has been already reported at the top of this comment.