Reputation: 26538
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
Upvotes: 3
Views: 2658
Reputation: 555
Not sure I have your question right but look for the CellInNamedrange function here http://www.cpearson.com/excel/excelM.htm
Upvotes: 1
Reputation: 29155
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
Upvotes: 1
Reputation: 7941
not very efficient but it should work.
private bool IsActiveCellInRange(Range selectedRange)
{
foreach cell in selectedRange.Cells
if (cell == activeCell)
return true;
}
return false;
}
Upvotes: 1