views:

49

answers:

2

I have an excel sheet, in which i have multiple cells selected, which are not adjacent to each other. When the user click on the button, we need to read all the cells data, process it and write it to some other cell.

If the cells are adjacent to each other, i was able to get the range and able to perform the operation. But if the cells are not adjacent to each other, i am not able to get the range. The Selection.Range is always giving the address of the last cell we selected.

But we need to get the addresses of all Cells, which i am not able to do it.

Please can anybody suggest me a way to handle this scenario.

Sample code:

Range objRange = (Range) Globals.ThisAddIn.Application.Selection;
                int nColCount = objRange.Columns.Count;
                int nRowCount = objRange.Rows.Count;

Vinay,

I have tried this code based on your suggestion,

 Range objRange = (Range) Globals.ThisAddIn.Application.Selection;

        foreach (Range cell in objRange)
        {
            MessageBox.Show("" + cell.Value2);
        }

But it didn't worked. Always it's giving the last selected cell. i have selected A1, A4, A13, A16 cells. But this code is returning the A16 Cell Value Only.

A: 

Range inherits from IEnumerable. So you can use for each iterator to enumerate via all cells. See the equivalent VBA code below:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim result As String
  result = ""
  Dim c As Range
  For Each c In Me.Application.Selection
    result = result & ", " & c.Text
  Next c
  Me.Cells.Item(1, 1).Value = result 
End Sub

You can always use Range.Row, Range.Column for getting the cell address.

As said in comments, use foreach synatx:

foreach(Range cell in objRange)
{
   // now access cell's properties - c.Value will give value
}
VinayC
Basically, this will fetch the data from one cell or list of continuous cells. But my problem is different. It's more like, For example if the user selected few cells A12, D34, C1. How can we automatically get the data from these cells, without hard coding these cell addresses in to the code.
Dinesh
There is no hard-coding done in above example to read the selection. Selection is an range object and you can enumerate via it. It doesn't matter if cells are continuous or not, each cell will be enumerated.Try pasting above VBA code in an excel worksheet and selected cell contents would be displayed in A1 cell.
VinayC
I have already tried the same code in c#. I am also used the same thing Application.Selection. But it's taking the data from the last cell only. The remaining cells it's not displaying the data.
Dinesh
Can you edit your question to paste the C# code that access the selection?
VinayC
I have added the sample code. I am using that code to get the selected cells. then i am using Iterator to read cell by cell. This is working fine if the cells are continuous, But If the cells not continuous then it's not working. It's always returning only one cell.
Dinesh
You haven't specified how you use the iterator. You must use for each syntax (and not for loop). I have updated my answer to illustrate use of foreach syntax in c#.
VinayC
A: 

After trying alot, I got the answer.

Here is the working code,

Areas objAreas = (Areas)objRange.Areas;
foreach (Range area in objAreas)
{
   string CellAddress = (GetExcelColumnName(area.Column) + "" + area.Row);
   MessageBox.Show(CellAddress);
}

GetExcelColumnName is the custom function u have written to convert Column number to Column Code(like a, b,... aa, ab.. etc)

Dinesh