tags:

views:

104

answers:

3

I am workin with VSTO. In this there is asituation in which on the button click of the Excel sheet it will display the input box and I will get the selected range. I want to know how can I get the selected range from inputbox hac the active cell address of the Excel sheet.

object objInputBox= Excel.Application.InputBox(prompt, field, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, 8);
ExcelXP.Range selectedRange = objInputBox as Excel.Range;

string rangeName = selectedRange.get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing); 

string activeCell =Application.Application.ActiveCell.get_Address(true, true, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);

How can I get the active cell address in rangeName?

Thanks in advance

+1  A: 

not very efficient but it should work.

private bool IsActiveCellInRange(Range selectedRange)
{
  foreach cell in selectedRange.Cells
    if (cell == activeCell)
      return true;
  }
  return false;
}
Nathan Fisher
+1  A: 

Not sure I have your question right but look for the CellInNamedrange funtion here http://www.cpearson.com/excel/excelM.htm

+4  A: 

Here's the way you can do it, using Application.Intersect. Example is VBA, but can easily be ported to C#.

Sub TestinRange()
    Dim inputRange As Range
    Set inputRange = Worksheets(1).Range("B1:B5")
    Dim IsActiveCellInInputRange As Range
    Set IsActiveCellInInputRange = Application.Intersect(inputRange, ActiveCell)
    If IsActiveCellInInputRange Is Nothing Then
        Debug.Print "Nope, the ActiveCell is not within the Input Range"
    Else
        Debug.Print "Yep, the ActiveCell is within the Input Range. ActiveCell Address: " & ActiveCell.Address
    End If
End Sub
Otaku