Retrieve the name of a cell in VB.NET

Add-in Express™ Support Service
That's what is more important than anything else

Retrieve the name of a cell in VB.NET
Equivalent of VBA cell.Name.Name 
Subscribe
Henri Pellemans




Posts: 7
Joined: 2023-08-10
Hi at ADX,

Usually my questions are not very difficult and I can solve them myself.

Using VS2022, ADX, Win 10 PRO 64-bit, Excel 365, all up to date

In Excel VBA I can retrieve the name of a cell by cell.Name.Name

How do I do this in VB.NET? The code cell.Name.ToString does not work

I know how to iterate through a list of names, but that is not what I want.

I asked chatGPT but it comes with cell.Name.Name but IntelliSense does not allow the second .Name

Any idea? thanks in advance.

Henri
Posted 17 Jun, 2024 16:37:24 Top
Andrei Smolin


Add-in Express team


Posts: 18965
Joined: 2006-05-11
Hello Henri,

The description at https://learn.microsoft.com/en-us/office/vba/api/excel.range.name is an obvious mistake because Range.Name returns an Excel.Name object or Nothing (null in C#).

To give a range of cells a name in the Excel UI, you should select the range and type its name in the Name box (see https://www.excelbaby.com/learn/excel-user-interface/). This associates an Excel.Name object with every cell of the range; now calling {a cell from the named range}.Name will return an Excel.Name, not Nothing. Only in this case calling Name.Name would have sense.

Excel VBA is targeted to late binding, while .NET prefers early binding. According to the VBA Object Browser, the return type of the Range.Name property is Variant. In .NET, Variant is translated to Object. That is, by calling Range.Name you get an Object (it represents an Excel.Name object; it can be Nothing). To be able to get the Name of the Excel.Name object, you should 1) make sure the object returned by Range.Name is not Nothing; 2) cast it to the Excel.Name type.

Regards from Poland (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 18 Jun, 2024 12:10:35 Top
Henri Pellemans




Posts: 7
Joined: 2023-08-10
Thanks Andrei,

I know that cell.Name returns an object. I always use Option Strict which disallows implicit conversion.

Casting to Excel.Name did the job. Part of the code (beta version):

Try
    result(counter, 9) = TryCast(cell.Name, Excel.Name).Name
Catch ex As Exception
    result(counter, 9) = "Henri"
End Try


Regards from the Netherlands
Posted 18 Jun, 2024 13:29:34 Top
Andrei Smolin


Add-in Express team


Posts: 18965
Joined: 2006-05-11
Great!

Regards from Poland (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 18 Jun, 2024 14:10:02 Top