Jsncrdnl
Jsncrdnl

Reputation: 3075

Get all cells coordinates for a Microsoft.Office.Interop.Excel.Range

Is there any method/property that I could use to get a string array of range's selected items ?

In example :
The range $B$2:$C$3 would give a string array with the following values : $B$2 $B$3 $C$2 $C$3

EDIT ::
I found the answer to my question on this post.
In fact, I need to use the following procedure :

    foreach (Range c in myRange)
    {
        string changedCell = c.get_Address(
            Type.Missing, Type.Missing, 
            XlReferenceStyle.xlA1, 
            Type.Missing, Type.Missing
        );  
        MessageBox.Show("Address:" + changedCell + " Value: " + c.Value2);
    }

Upvotes: 1

Views: 2400

Answers (1)

shahkalpesh
shahkalpesh

Reputation: 33474

AFAIK, there isn't a property that can give cell addresses of a given range in the format, you are expecting.

Here is the VBA code to get what you are looking for

Function GetCellAddresses(ByVal srcRange As Range) As Variant
Dim cellAddresses As Variant

Dim cellCtr As Long
Dim cellCount As Long

Dim cell As Range

If Not srcRange Is Nothing Then
    cellCtr = 1
    cellCount = srcRange.Cells.Count
    ReDim cellAddresses(1 To cellCount)

    For Each cell In srcRange.Cells
        cellAddresses(cellCtr) = cell.Address
        cellCtr = cellCtr + 1
    Next
End If

GetCellAddresses = cellAddresses
End Function

To call the above, GetCellAddresses(sheet1.Range("A1:B3"))

EDIT: Using LINQ, this can become 1 or 2 liner.
For e.g. var cellAddresses = srcRange.Select(c => c.Address);

OR

from cell in srcRange select cell.Address

Upvotes: 1

Related Questions